Instructions:
Write a program with total change amount (in cents) 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.
Hint: Start from the largest denomination. Divide the change amount by the number of cents in the denomination to get the number of coins used. The remainder of the division is used to find the number of coins used in the next denomination. Ex: value // 100 gives the number of dollars used. The remainder is divided by 25 to find the number of quarters used.
Examples of expected output with inputs():
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
The code I've written so far:
# TODO: Input the total change amount
totalChange = int(input())
# TODO: Test if the input is valid. Output the result if the input is not valid.
# TODO: Calculate the number of dollars
# TODO: Calculate the number of quarters
# TODO: Calculate the number of dimes
# TODO: Calculate the number of nickels
# TODO: Calculate the number of pennies
# TODO: Output the number of each coin used. Use if-else statements to output the correct singular and plural form.
if totalChange == 0:
print('No Change')
dollars = totalChange // 100
if totalChange > 100:
if dollars == 1:
print(dollars, 'Dollar')
else:
print(dollars, 'Dollars')
totalChange = totalChange % 100
quarters = totalChange // 25
if totalChange > 25:
if quarters == 1:
print(quarters, 'Quarter')
else:
print(quarters, 'Quarters')
totalChange = totalChange % 25
dimes = totalChange // 10
if totalChange > 10:
if dimes == 1:
print(dimes, 'Dime')
else:
print(dimes, 'Dimes')
totalChange = totalChange % 10
nickels = totalChange // 5
if totalChange > 5:
if nickels == 1:
print(nickels, 'Nickel')
else:
print(nickels, 'Nickels')
totalChange = totalChange % 5
pennies = totalChange // 1
if totalChange > 1:
if pennies == 1:
print(pennies, 'Penny')
else:
print(pennies, 'Pennies')
totalChange = totalChange - pennies
It works well enough that I am getting partial credit in the zybook that it came from, but I just don't understand what the hell is going wrong. It absolutely refuses to print the pennies portion of the code.