Difference between revisions of "CSC111 Blocking Code into Functions"
(Created page with "--~~~~ ---- =Version 1= <br /> <source lang="python"> # ATM_version1.py # D. Thiebaut # this program distributes some amount of dollars # into bills and coins. # amount = 124...") |
(→Output) |
||
Line 55: | Line 55: | ||
0 $5-bill(s) | 0 $5-bill(s) | ||
4 $1-bill(s) | 4 $1-bill(s) | ||
+ | |||
1 quarter(s) | 1 quarter(s) | ||
2 dime(s) | 2 dime(s) |
Revision as of 22:16, 6 February 2014
--D. Thiebaut (talk) 21:16, 6 February 2014 (EST)
Contents
Version 1
# ATM_version1.py
# D. Thiebaut
# this program distributes some amount of dollars
# into bills and coins.
#
amount = 124.45
bAmount = int( amount )
cAmount = int( amount * 100 ) % 100
#print( amount, bAmount, cAmount )
print( "Distributing $" + str( amount ) )
no20s = bAmount // 20
bAmount = bAmount % 20
no10s = bAmount // 10
bAmount = bAmount % 10
no5s = bAmount // 5
no1s = bAmount % 5
print( no20s, "$20-bill(s)" )
print( no10s, "$10-bill(s)" )
print( no5s, "$5-bill(s)" )
print( no1s, "$1-bill(s)" )
no25s = cAmount // 25
cAmount = cAmount % 25
no10s = cAmount // 10
cAmount = cAmount % 10
no5s = cAmount // 5
no1s = cAmount % 5
print( no25s, "quarter(s)" )
print( no10s, "dime(s)" )
print( no5s, "nickel(s)" )
print( no1s, "cent(s)" )
Output
>>>
Distributing $124.45
6 $20-bill(s)
0 $10-bill(s)
0 $5-bill(s)
4 $1-bill(s)
1 quarter(s)
2 dime(s)
0 nickel(s)
0 cent(s)
Version 2
# ATM_version2.py
# D. Thiebaut
# this program distributes some amount of dollars
# into bills and coins.
#
def distribute( amount, d1, d2, d3, d4 ):
n1 = amount // d1
amount = amount % d1
n2 = amount // d2
amount = amount % d2
n3 = amount // d3
amount = amount % d3
n4 = amount % d4
print( "%d unit(s) of %d" % ( n1, d1 ) )
print( "%d unit(s) of %d" % ( n2, d2 ) )
print( "%d unit(s) of %d" % ( n3, d3 ) )
print( "%d unit(s) of %d" % ( n4, d4 ) )
amount = 124.45
bAmount = int( amount )
cAmount = int( amount * 100 ) % 100
print( "Distributing $" + str( amount ) )
print()
distribute( bAmount, 20, 10, 5, 1 )
print()
distribute( cAmount, 25, 10, 5, 1 )
Output
>>>
Distributing $124.45
6 unit(s) of 20
0 unit(s) of 10
0 unit(s) of 5
0 unit(s) of 1
1 unit(s) of 25
2 unit(s) of 10
0 unit(s) of 5
0 unit(s) of 1