Difference between revisions of "CSC111 Blocking Code into Functions"

From dftwiki3
Jump to: navigation, search
(Version 1)
(Version 1)
 
Line 10: Line 10:
 
# into bills and coins.
 
# into bills and coins.
 
#
 
#
 +
# Original amount
 
amount = 124.45
 
amount = 124.45
 +
 +
# transform into two integer amounts, one for bills, one for coins
 
bAmount = int( amount )
 
bAmount = int( amount )
 
cAmount = int( amount * 100 ) % 100
 
cAmount = int( amount * 100 ) % 100
  
#print( amount, bAmount, cAmount )
 
 
print( "Distributing $" + str( amount ) )
 
print( "Distributing $" + str( amount ) )
  
 +
# distribute amount in different denominations
 
no20s = bAmount // 20
 
no20s = bAmount // 20
 
bAmount = bAmount % 20
 
bAmount = bAmount % 20
Line 26: Line 29:
 
no1s = bAmount % 5
 
no1s = bAmount % 5
  
 +
# display resulting bill distribution
 
print( no20s, "$20-bill(s)" )
 
print( no20s, "$20-bill(s)" )
 
print( no10s, "$10-bill(s)" )
 
print( no10s, "$10-bill(s)" )
Line 31: Line 35:
 
print( no1s,  "$1-bill(s)" )
 
print( no1s,  "$1-bill(s)" )
  
 +
# distribute coins into different quantities
 
no25s = cAmount // 25
 
no25s = cAmount // 25
 
cAmount = cAmount % 25
 
cAmount = cAmount % 25
Line 40: Line 45:
 
no1s = cAmount % 5
 
no1s = cAmount % 5
  
 +
# display resulting coin distribution
 
print()
 
print()
 
print( no25s, "quarter(s)" )
 
print( no25s, "quarter(s)" )

Latest revision as of 22:18, 6 February 2014

--D. Thiebaut (talk) 21:16, 6 February 2014 (EST)


Version 1


# ATM_version1.py
# D. Thiebaut
# this program distributes some amount of dollars
# into bills and coins.
#
# Original amount
amount = 124.45

# transform into two integer amounts, one for bills, one for coins
bAmount = int( amount )
cAmount = int( amount * 100 ) % 100

print( "Distributing $" + str( amount ) )

# distribute amount in different denominations
no20s = bAmount // 20
bAmount = bAmount % 20

no10s = bAmount // 10
bAmount = bAmount % 10

no5s = bAmount // 5
no1s = bAmount % 5

# display resulting bill distribution 
print( no20s, "$20-bill(s)" )
print( no10s, "$10-bill(s)" )
print( no5s,  "$5-bill(s)" )
print( no1s,  "$1-bill(s)" )

# distribute coins into different quantities
no25s = cAmount // 25
cAmount = cAmount % 25

no10s = cAmount // 10
cAmount = cAmount % 10

no5s = cAmount // 5
no1s = cAmount % 5

# display resulting coin distribution 
print()
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