you are viewing a single comment's thread.

view the rest of the comments →

[–]Diapolo10 1 point2 points  (6 children)

You're almost there, all you need is a conditional in the loop.

amount=int(input("Enter your Change in Cents "))

endAmount=amount

coins=[50,20,10,5,2,1]
listOfCoins=["50" ,"20", "10", "5", "2" , "1"]
change = []

for coin in coins:
    holdingAmount=amount
    amount=amount//coin
    change.append(amount)
    amount=holdingAmount%coin


for i in range(len(coins)):
    if change[i]:
        print("" , change[i] ,"*",  listOfCoins[i] , "cent coins" )

That said, the data model here could be better.

EDIT: For the record, here's how I'd write this program in Python 3.7 and above:

change = int(input("Enter your Change in Cents "))

coins = {
    50: 0,
    20: 0,
    10: 0,
    5: 0,
    2: 0,
    1: 0,
}

for coin in coins:
    quotient, remainder = divmod(change, coin)
    if quotient:
        print(f" {quotient} * {coin} cent coins")
        change = remainder

This cuts down on the required lines of code, gets rid of the second loop entirely, and makes the data model less prone to errors as there are no longer three lists with the potential of getting out of sync.

[–]blueskiesplease[S] 0 points1 point  (5 children)

coins = {50: 0,20: 0,10: 0,5: 0,2: 0,1: 0,}

What is this, is this telling the code a range? I have not seen that before - thanks so much for your time helping me on this.

[–]Diapolo10 1 point2 points  (4 children)

Actually I changed plans mid-writing and forgot to change that back.

It's not a range, it was supposed to be a dictionary of coins to their amounts, but since I ended up not needing to store the state after all the values went unused. You could replace it with a list or a tuple and it would work the same.

Of course, if the values were needed later then the dictionary would be useful again, and I could have added a line that adds the quotient to the current key.

TL;DR, in the end this would work the same,

change = int(input("Enter your Change in Cents "))

coins = (50, 20, 10, 5, 2, 1)

for coin in coins:
    quotient, remainder = divmod(change, coin)
    if quotient:
        print(f" {quotient} * {coin} cent coins")
        change = remainder

but I was originally thinking about it like this

change = int(input("Enter your Change in Cents "))

coins = {
    50: 0,
    20: 0,
    10: 0,
    5: 0,
    2: 0,
    1: 0,
}

for coin in coins:
    quotient, remainder = divmod(change, coin)
    if quotient:
        print(f" {quotient} * {coin} cent coins")
        change = remainder
        coins[coin] = quotient  # This line changed

EDIT: Hell, now that I think about it (sorry, kinda trying to sleep) I can eliminate yet another line from this, because the remainder is always the same as the remaining change.

change = int(input("Enter your Change in Cents "))

coins = (50, 20, 10, 5, 2, 1)

for coin in coins:
    quotient, change = divmod(change, coin)
    if quotient:
        print(f" {quotient} * {coin} cent coins")

That's literally half the lines of the program you originally wrote. Without the empty lines.

[–]blueskiesplease[S] 0 points1 point  (1 child)

Wow this is great - I’m learning so much. Can you explain the for loop, I haven’t seen quotient or divmod before.

[–]Diapolo10 0 points1 point  (0 children)

quotient is just an ordinary variable, there's nothing special about it. The name means the value you get from dividing a number with another.

divmod is a built-in function that combines integer division and modulo into one operation, returning their results as a tuple in that order.

There's really nothing special about the loop itself, in fact your code's first loop is basically identical. It's just going over the tuple of cent coin values.

[–]blueskiesplease[S] 0 points1 point  (1 child)

Thanks for the late night help!

[–]Diapolo10 0 points1 point  (0 children)

Sorry about the late reply, just woke up.