In: Computer Science
USE PYTHON
4.15 LAB: Exact change
Write a program with total change amount as an integer input, and output the change using the fewest coins, one coin type per line. The coin types are Dollars, Quarters, Dimes, Nickels, and Pennies. Use singular and plural coin names as appropriate, like 1 Penny vs. 2 Pennies.
Ex: If the input is:
0
(or less than 0), the output is:
No change
Ex: If the input is:
45
the output is:
1 Quarter 2 Dimes
def exactChange(userTotal): if (userTotal <= 0): return None else: numDollars = userTotal // 100 userTotal %= 100 numQuarters = userTotal // 25 userTotal %= 25 numDimes = userTotal // 10 userTotal %= 10 numNickels = userTotal // 5 numPennies = userTotal % 5 return (numDollars, numQuarters, numDimes, numNickels, numPennies) # Testing def main(): n = int(input()) res = exactChange(n) if(res == None): print("No change") else: numDollars = res[0] numQuarters = res[1] numDimes = res[2] numNickels = res[3] numPennies = res[4] if numDollars > 0: if numDollars == 1: print('%d Dollar' % numDollars) else: print('%d Dollars' % numDollars) if numQuarters > 0: if numQuarters == 1: print('%d Quarter' % numQuarters) else: print('%d Quarters' % numQuarters) if numDimes > 0: if numDimes == 1: print('%d Dime' % numDimes) else: print('%d Dimes' % numDimes) if numNickels > 0: if numNickels == 1: print('%d Nickel' % numNickels) else: print('%d Nickels' % numNickels) if numPennies > 0: if numPennies == 1: print('%d Penny' % numPennies) else: print('%d Pennies' % numPennies) main()