Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Why does the program skip over the last function?

+5 votes
asked Mar 19, 2022 by Joey (540 points)

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.

2 Answers

0 votes
answered Mar 19, 2022 by Joey (540 points)
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.
+1 vote
answered Mar 19, 2022 by Peter Minarik (84,720 points)

The problem is that you're checking if the given coin type has more than one occurrence, while you should check if it has at least one occurrence.

To fix it for the pennies, you should change your code like this:

if totalChange >= 1:

I'll let you fix the other denominations.

commented Mar 20, 2022 by Joey (540 points)
... my face when I read this... I feel dumb XD
I just went and fixed it before I finished this comment and it absolutely worked. I greatly appreciate it. I'm great when it comes to the more advanced stuff but I always have trouble with the simple stuff... Thanks amigo
commented Mar 20, 2022 by Peter Minarik (84,720 points)
No problemo.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...