Angie
Angie

Reputation: 41

Return Change: How do I remove a variable (dollars, quarters, dimes, nickels, pennies) from "change" if the variable =0?

Zybooks 4.18 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.

I want to know if there's anything I can use in place of change.remove() to remove that variable from "change." I know that change.remove() doesn't work but put it there to show what it is that I want to do. I'm taking my first Computer Science class right now, so keep in mind that there are a lot of things I might not have learned yet. I know that this problem has been solved before on here before, but I'm trying to write a code with what I have learned so far. Is there any way that I could do this problem using what I have and only fixing a few elements? This is what I have right now:

x=int(input())
if x<=0:
    print('No change')
else:
    num_of_dollars = x//100
    x = x-num_of_dollars*100
    num_of_quarters = x//25
    x = x-num_of_quarters*25
    num_of_dimes = x//10
    x = x-num_of_dimes*10
    num_of_nickels = x//5
    x = x-num_of_nickels*5
    num_of_pennies = x//1

    change = dollars+'\n'+quarters+'\n'+dimes+'\n'+nickels+'\n'+pennies

    if num_of_dollars>1:
        dollars = str(num_of_dollars)+' Dollars'
    elif num_of_dollars==1:
        dollars = str(num_of_dollars)+' Dollar'
    elif num_of_dollars==0:
        change.remove(dollars)
    
    if num_of_quarters>1:
        quarters = str(num_of_quarters)+' Quarters'
    elif num_of_quarters==1:
        quarters = str(num_of_quarters)+' Quarter'
    elif num_of_quarters==0:
        change.remove(quarters)

    if num_of_dimes>1:
        dimes = str(num_of_dimes)+' Dimes'
    elif num_of_dimes==1:
        dimes = str(num_of_dimes)+' Dime'
    elif num_of_dimes==0:
        change.remove(dimes)
    
    if num_of_nickels>1:
        nickels = str(num_of_nickels)+' Nickels'
    elif num_of_nickels==1:
        nickels = str(num_of_nickels)+' Nickel'
    elif num_of_nickels==0:
        change.remove(nickels)
    
    if num_of_pennies>1:
        pennies = str(num_of_pennies)+' Pennies'
    elif num_of_pennies==1:
        pennies = str(num_of_pennies)+' Penny'
    elif num_of_pennies==0:
        change.remove(pennies)

    print(change)

Upvotes: 2

Views: 464

Answers (1)

Joffan
Joffan

Reputation: 1480

It seems simpler to add to change as you go, rather than removing anything, particularly as its initial assignment there really shouldn't work and retains no connection to the variables that you use to compose change.

So for example

    change = ``

    if num_of_dollars > 1:
        dollars = str(num_of_dollars)+' Dollars\n'
    elif num_of_dollars == 1:
        dollars = str(num_of_dollars)+' Dollar\n'
    if num_of_dollars > 0:
        change += dollars
    

etc - I'm trying to keep your structure as much as possible.

: :

But for when you know about lists, and lists of lists:

coins = [["dollar", "dollars", 100], ["quarter", "quarters", 25], ["dime", "dimes", 10], 
         ["nickel", "nickels", 5], ["penny", "pennies", 1]]

x = int(input("Change amount in pennies: "))
out = ""
for name, name_pl, val in coins:
    n_coin, x = divmod(x, val)
    if n_coin > 0:
        if len(out) > 0: out += "\n"
        if n_coin == 1:
            out += "1 " + name
        else:
            out += str(n_coin) + " " + name_pl
if len(out) > 0:
    print(out)
else:
    print("No change")

And if you happen to be curious about when a greedy algorithm works to give the fewest coins and when it doesn't, I discussed that on maths.stackexchange

Upvotes: 2

Related Questions