'Exact Change in Python

I am trying to write a code in Python to where it outputs exact change using the fewest coins and one coin type per line. The coin types are Dollars, Quarters, Dimes, Nickels, and Pennies. I also have to use singular and plural coin names as appropriate, like 1 Penny vs. 2 Pennies. When I input 45 and ran the code, I got an error saying (Your program produced no output). Here is my code:

total_change = int(input())

if total_change <= 0:
print('No change')

if total_change >= 100:
dollar = total_change//100
dollar_change = total_change % 100
if dollar == 1:
    print(dollar + ' Dollar')
elif dollar > 1:
    print(dollar + ' Dollars')
    
elif dollar_change >= 25:
    quarter = dollar_change//25
    quarter_change = dollar_change % 25
    if quarter == 1:
        print(quarter + ' Quarter')
    elif quarter > 1:
        print(quarter + ' Quarters')
        
    elif quarter_change >= 10:
        dime = quarter_change // 10
        dime_change = quarter_change % 10
        if dime == 1:
            print(dime + ' Dime')
        elif dime > 1:
            print(dime + ' Dimes')
            
        elif dime_change >= 5:
            nickel = dime_change // 5
            nickel_change = dime_change % 5
            if nickel == 1:
                print(nickel + ' Nickel')
            elif nickel > 1:
                print(nickel + ' Nickels')
                
        elif nickel_change >= 1:
                penny = nickel_change // 1
                if penny == 1:
                    print(penny + ' Penny')
                else:
                    print(penny + ' Pennies')
                
                


Solution 1:[1]

Your code has numerous problems that needed to be resolved, including the lack of a condition for input values 0 < total_change < 100, problems with indentation (the elif blocks should be aligned), unnecessary variables (you do not need variables like nickel_change and dime_change - total_change is all that matters), and you tried to print dollar + ' Dollar' even though dollar was a numeric variable.

I wanted to improve on the issues in your code and make it, well, functional, but without entirely rewriting your work. So, the basic framework of the code I'm going to provide is the same.

I used the method of recursion. I have the following function with all of your (cleaned) code:

def printCurrency(total_change):
    dollar = total_change//100
    dollar_change = total_change % 100
    if dollar == 1:
        print(str(dollar) + ' Dollar')
        printCurrency(total_change-1*100)
    elif dollar > 1:
        print(str(dollar) + ' Dollars')
        printCurrency(total_change-dollar*100)
    elif dollar_change >= 25:
        quarter = dollar_change//25
        quarter_change = dollar_change % 25
        if quarter == 1:
            print(str(quarter) + ' Quarter')
            printCurrency(total_change-1*25)
        elif quarter > 1:
            print(str(quarter) + ' Quarters')
            printCurrency(total_change-quarter*25)
    elif dollar_change >= 10:
        dime = dollar_change // 10
        dime_change = dollar_change % 10
        if dime == 1:
            print(str(dime) + ' Dime')
            printCurrency(total_change-1*10)
        elif dime > 1:
            print(str(dime) + ' Dimes')
            printCurrency(total_change-dime*10)
    elif dollar_change >= 5:
        nickel = dollar_change // 5
        nickel_change = dollar_change % 5
        if nickel == 1:
            print(str(nickel) + ' Nickel')
            printCurrency(total_change-1*5)
        elif nickel > 1:
            print(str(nickel) + ' Nickels')
            printCurrency(total_change-nickel*5)
    elif dollar_change >= 1:
            penny = dollar_change // 1
            if penny == 1:
                print(str(penny) + ' Penny')
                printCurrency(total_change-1*1)
            else:
                print(str(penny) + ' Pennies')
                printCurrency(total_change-penny*1)

Notice how every time a line is printed, the function is ran again but after subtracting out the change we've already processed.

A few examples:

>>> printCurrency(45)
1 Quarter
2 Dimes
>>> printCurrency(101)
1 Dollar
1 Penny
>>> printCurrency(349)
3 Dollars
1 Quarter
2 Dimes
4 Pennies

And to tie this into your original framework with an input...

total_change = int(input())

if total_change <= 0:
    print('No change')

if total_change >= 0:
    printCurrency(total_change)

Let me know if you have any questions about the changes I've made to your code!

Solution 2:[2]

total = int(input())
if total == 0:
    print("No change")
else:
    denominations = [(100, "Dollar", "Dollars"), (25, "Quarter", "Quarters"), (10, "Dime", "Dimes"), (5, "Nickel", "Nickels"), (1, "Penny", "Pennies")]
    for d in denominations:
        coins = total // d[0]
        total %= d[0]
        if coins > 1:
            print(f"{coins} {d[2]}")
        elif coins == 1:
            print(f"1 {d[1]}")

In this answer, I created a list full of tuples of all the coins and their values in cents. I then created a for loop which runs through all the tuples in the list, dividing the total removing the remainder for the number of coins

like this:

*coins = total // d[0] #returns the number of coins of the current iteration

Then, in order for the loop to continue to the next iteration and do the calculations correctly, I set the total in cents equal to the remainder of the total divided by the current iteration.

like this:

total %= d[0] #can also be written as total = total % d[0]

Then, I take the number of coins and check if the value is greater than one. If the conditional is met, it prints the number of coins followed by the corresponding "plural version" of the word.

like this:

if coins > 1:
    print(f"{coins} {d[2]}") 
#d[2] refers to the third item in the tuple of the current iteration

Finally, I use an else-if conditional to return 1 plus the "singular version" of the word

like this:

elif coins == 1:
    print(f"1 {d[1]}")
#d[1] refers to the second item in the tuple of the current iteration

Solution 3:[3]

This is more of an answer that zybooks is looking for considering what it has taught up to this point. All that I have done here is decrement total_change each time I go down the list of coins. If there were no coins for that set then print a statement on the previous line. total_change = int(input())

if total_change <= 0:
    print('No change')
else:
    dollar = total_change // 100
    if dollar == 1:
        print(dollar, 'Dollar')
        total_change = total_change - (dollar * 100)
    elif dollar <= 0:
        print(end='')
    else:
        print(dollar, 'Dollars')
        total_change = total_change - (dollar * 100)
    quarter = total_change // 25
    if quarter == 1:
        print(quarter, 'Quarter')
        total_change = total_change - (quarter * 25)
    elif quarter <= 0:
        print(end='')
    else:
        print(quarter, 'Quarters')
        total_change = total_change - (quarter * 25)
    dime = total_change // 10
    if dime == 1:
        print(dime, 'Dime')
        total_change = total_change - (dime * 10)
    elif dime <= 0:
        print(end='')
    else:
        print(dime, 'Dimes')
        total_change = total_change - (dime * 10)
    nickel = total_change // 5
    if nickel == 1:
        print(nickel, 'Nickel')
        total_change = total_change - (nickel * 5)
    elif nickel <= 0:
        print(end='')
    else:
        print(nickel, 'Nickels')
        total_change = total_change - (nickel * 5)
    penny = total_change // 1
    if penny == 1:
        print(penny, 'Penny')
        total_change = total_change - (penny * 1)
    elif penny <= 0:
        print(end='')
    else:
        print(penny, 'Pennies')
        total_change = total_change - (penny * 1)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Machetes0602
Solution 2 Charlie
Solution 3