Oct-01-2017, 05:18 PM
(This post was last modified: Oct-01-2017, 05:49 PM by gruntfutuk.)
I find it a little easier to use zip on the two lists in the for loop to avoid indexing the lists.
(Also, changed / to // as OP appears to be using Python 3, and simplified the answer string generation)
Couldn't resist (I'm bored) tweaking, and demonstrating the point @Krookroo made about how easy it is to change currency when using two lists:
(Also, changed / to // as OP appears to be using Python 3, and simplified the answer string generation)
currency_values = [25, 10, 5, 1] currency_names = ['quarter', 'dime', 'nickel', 'cent'] cents_to_convert = int(input('Hey little boy how much cents do you want me to convert?\n')) final_answer = "I'll give you " for name, currency in zip(currency_names, currency_values): if cents_to_convert<=0: #if we have converted all the cents, just end and give the result. break if cents_to_convert>=currency: #if less, we don't need to print that we give this guy 0 of this coin. current_currency_number = cents_to_convert//currency final_answer += f' {current_currency_number} {name} ' cents_to_convert = cents_to_convert % currency print(final_answer)
Couldn't resist (I'm bored) tweaking, and demonstrating the point @Krookroo made about how easy it is to change currency when using two lists:
#!/usr/bin/env python3 # simple programme to calculate largest denominations of change to give # denominations lists, values and names one-to-one mapping! # sterling currency_values = [200, 100, 50, 20, 10, 5, 2, 1] currency_names = ['two pound', 'pound', 'fifty pence', 'twenty pence', 'ten pence', 'five pence', 'two pence', 'one pence'] def breakdown(amountstr): if not amountstr: return "" change = "" amount = int(amountstr) for name, currency in zip(currency_names, currency_values): if amount <= 0: # end if no change left break if amount >= currency: # we have some of this denomination to pay quantity = amount // currency change += f'\n{quantity}x \t{name} ' amount = amount % currency return change while True: change = breakdown(input('\nHow much needs to be returned in change (return to exit)? ')) if change: print(f'Change breakdown is: {change}') else: breakAlso available on repl.it.
I am trying to help you, really, even if it doesn't always seem that way