128k views
2 votes
Write a program to assist a cashier with determining correct change. The program should have one input, the number of cents to return to the customer. The output should be the appropriate change in quarters, dimes, nickels and cents.

User Marengaz
by
8.0k points

1 Answer

5 votes
```
#!/usr/local/bin/python3

import sys

coins = { "quarters" : 25, "dimes" : 10, "nickels" : 5, "pennies" : 1 }

def mkChange( balance, coin ):
qty = balance // coins[ coin ]
if( qty ):
print( str( qty ) + ' ' + coin )
return( balance % coins[ coin ] )

if( __name__ == "__main__" ):
if( len( sys.argv ) == 2 ):
balance = int( sys.argv[ 1 ] )
balance = mkChange( balance, "quarters" )
balance = mkChange( balance, "dimes" )
balance = mkChange( balance, "nickels" )
balance = mkChange( balance, "pennies" )
else:
sys.stderr.write( "\\usage: " + sys.argv[ 0 ] + " <change owed>\\" )
```


User Neeraj Gupta
by
7.6k points

No related questions found