Difference between revisions of "CSC111 Programs Created in Class 2018"
Line 232: | Line 232: | ||
</source> | </source> | ||
+ | |||
+ | <br /> | ||
+ | =2/12/18= | ||
+ | <br /> | ||
+ | ::<source lang="python"> | ||
+ | # teller.py | ||
+ | # D. Thiebaut | ||
+ | # simulates teller machine program | ||
+ | # prompts user, displays number of bills | ||
+ | |||
+ | # input amount to withdraw (use eval) | ||
+ | amount = eval( input( "Amount to withdraw? " ) ) | ||
+ | amount = abs( amount ) | ||
+ | print( "Amount:", amount ) | ||
+ | |||
+ | |||
+ | # compute number of bills, 20, 10, 5, 1 | ||
+ | no20s = amount // 20 | ||
+ | amount = amount % 20 # leftover after giving out all the 20s | ||
+ | |||
+ | no10s = amount // 10 | ||
+ | amount = amount % 10 # leftover after giving out all the 10s | ||
+ | |||
+ | no5s = amount // 5 | ||
+ | amount = amount % 5 # leftover after giving out all the 5s | ||
+ | |||
+ | no1s = amount | ||
+ | |||
+ | |||
+ | # display number of bills | ||
+ | print( no20s, "$20-bill(s)" ) | ||
+ | print( no10s, "$10-bill(s)" ) | ||
+ | print( no5s, "$5-bill(s)" ) | ||
+ | print( no1s, "$1-bill(s)" ) | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | [[Category:Python]][[Category:CSC111]] |
Revision as of 13:21, 12 February 2018
D. Thiebaut (talk) 12:30, 31 January 2018 (EST)
Contents
1/30/18
# wedWeek1.py
# D. Thiebaut
# Demo program week 1
# variables
age = 20
year = 2018
# compute year born
print( year - age )
# example of for loop
for name in [ "smith", 23, 3.14159, "hello" ]:
print( name )
"""
challenge: print the following lines
***
Mae
*****
Alice
*******
Felicia
"""
for name in [ "Mae", "Alice", "Felicia" ]:
print( '*' * len( name ) )
print( name )
"""
new challenge: print the following lines
*
Mae
********
Alice
****
Felicia
**
"""
for name in [ "*", "Mae", "********", "Alice", "****", "Felicia", "**" ]:
print( name )
02/05/18
Table of Fahrenheit to Celsius conversion
# 020518.py # D. Thiebaut # # print table of temperatures # start at 100 F down to -30 F # in steps of 10 # using the formula: Celsius = (Farhenheit - 32 ) * 5 / 9 # print the header print( "Fahrenheit --> Celsius" ) # display the table of temperatures # ranging from 100 down to -30F. for fTemp in range( 100, -31, -10 ): cTemp = ( fTemp - 32 ) * 5 / 9 print( fTemp, '-->', cTemp )
Problem: get user name and grade and display user information and grade as a bar graph.
# barGraph.py
# D. Thiebaut
# this program prompts the user for her information
# and grade and displays a bar-graph.
#
#First name? Dominique
#Last name? Thiebaut
#Id? 990123456
#Final grade? 90
#
#+———————————————————————————————————-———————————-+
#|Dominique Thiebaut 990123456 |
#+———————————————————————————————————-———————————-+
# 00...10...20...30...40...50...60...70...80...90...100
#grade: #############################################
#class: ########################################
#
#
# input section
fName = input( "First name? " )
lName = input( "Last name? " )
Id = input( "Id? " )
final = input( "Final grade? " )
# output section
print( fName, lName, Id )
bar = "+———————————————————————————————————-———————————-+"
barLen = len( bar )
print( "barLen =", barLen )
nameIdLen = len( fName )+1+len( lName ) + len( Id )+1
print( "nameIdLen =", nameIdLen )
print( bar )
print( "|", fName, lName, ' '*(barLen-nameIdLen), Id, '|' )
print( bar )
2/7/18
We didn't have time to cover this problem, because of the snow storm... But it simply displays
an 8 by 8 chessboard using hash tags.
# chessBoard.py # D. Thiebaut # displays an 8x8 chessboard, as shown below. """ ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### """ numRows = eval( input( "Number of rows? " ) ) numCols = eval( input( "Number of columns? " ) ) white=" " black="###" bar1 = "" bar2 = "" for i in range( numCols//2 ): bar1 = bar1 + white + black bar2 = bar2 + black + white for i in range( numRows//2 ): # display 2 lines of squares # alternating the colors # 5 ### ### ### ### # ### ### ### ### # ### ### ### ### # ### ### ### ### # ### ### ### ### # ### ### ### ### for n in range( 3 ): print( bar1 ) for n in range( 3 ): print( bar2 )
2/9/18
# barGraph.py # D. Thiebaut # this program prompts the user for her information # and grade and displays a bar-graph. # #First name? Dominique #Last name? Thiebaut #Id? 990123456 #Final grade? 90 # #+———————————————————————————————————-———————————-+ #|Dominique Thiebaut 990123456 | #+———————————————————————————————————-———————————-+ # 00...10...20...30...40...50...60...70...80...90...100 #grade: ############################################# #class: ######################################## # # # box geometry bar = "+———————————————————————————————————-———————————-+" barLen = len( bar ) # input section fName = input( "First name? " ) fNameLen = len( fName ) lName = input( "Last name? " ) lNameLen = len( lName ) Id = input( "Id? " ) IdLen = len( Id ) numSpaces = barLen -3 -(fNameLen+1+lNameLen+IdLen) grade = eval( input( "Final grade? " ) ) classGrade = 80 # output section print( bar ) print( '|' + fName, ' ', lName, ' '*(numSpaces), Id, ' |', sep='' ) print( bar ) # print the scale print( " 00...10...20...30...40...50...60...70...80...90...100" ) # print the student grade as a bar numHashTags = grade // 2 print( "grade:", '#' * numHashTags ) #print the class grade as a bar numHashTags = classGrade // 2 print( "class:", '#' * numHashTags )
2/12/18
# teller.py # D. Thiebaut # simulates teller machine program # prompts user, displays number of bills # input amount to withdraw (use eval) amount = eval( input( "Amount to withdraw? " ) ) amount = abs( amount ) print( "Amount:", amount ) # compute number of bills, 20, 10, 5, 1 no20s = amount // 20 amount = amount % 20 # leftover after giving out all the 20s no10s = amount // 10 amount = amount % 10 # leftover after giving out all the 10s no5s = amount // 5 amount = amount % 5 # leftover after giving out all the 5s no1s = amount # display number of bills print( no20s, "$20-bill(s)" ) print( no10s, "$10-bill(s)" ) print( no5s, "$5-bill(s)" ) print( no1s, "$1-bill(s)" )