Sep-17-2017, 06:33 PM
Nice :)
Just add a little bit, here's how I would have done it. The idea is to store the data we use in lists so that we can very easily tweak the algorithm for another set of currencies, just by editing the lists containing the currencies and their values. The logic behind is the same as yours, expressed in a more general way.
There might be a cleaner / more efficient way to do this with dictionaries, but I never used dictionaries before (and won't for a while).
Just add a little bit, here's how I would have done it. The idea is to store the data we use in lists so that we can very easily tweak the algorithm for another set of currencies, just by editing the lists containing the currencies and their values. The logic behind is the same as yours, expressed in a more general way.
There might be a cleaner / more efficient way to do this with dictionaries, but I never used dictionaries before (and won't for a while).
currency_values = [25, 10, 5, 1] currency_names = ['quartet', '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 enumerate(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 += ' {0} {1} '.format(str(current_currency_number), currency_names[name]) cents_to_convert = cents_to_convert % currency print final_answer