|
Post by Admin on May 4, 2020 21:18:37 GMT -5
Write a program that begins by reading a number of cents from the user as an integer. Then your program should compute and display the denominations of the coins that should be used to give that amount of change to the shopper. The change should be given using as few coins as possible. Assume that the machine is loaded with pennies, nickels, dimes, quarters, loonies and toonies.
A one dollar coin was introduced in Canada in 1987. It is referred to as a loonie because one side of the coin has a loon (a type of bird) on it. The two dollar coin, referred to as a toonie, was introduced 9 years later. It’s name is derived from the combination of the number two and the name of the loonie.
payout = float(input('Please pay')) coins = [200,100,25,10,5,1] coins1 = ['pennies', 'nickels', 'dimes', 'quarters', 'loonies', 'toonies'] payout = payout * 100 denominations = 0
for change in coins: denominations = payout // change payout = payout % change if denominations == 0: coins1.pop() continue print ('%d %s' % (denominations, coins1.pop()))
|
|
|
Post by arnabbiswas on May 4, 2020 23:38:50 GMT -5
Doesnt work where coins = [200,100,25,15,1] and payout = 0.6
Returns:
2 quarters 10 nickels
But should return:
4 of 15 cents
Basically seems to break in certain cases where coins are of non standard denominations
|
|
|
Post by arnabbiswas on May 5, 2020 0:22:43 GMT -5
Version using dictionaries:
payout = float(input('Please pay')) coins = {200 : 'toonies', 100 : 'loonies', 25 : 'quarters', 10 : 'dimes', 5 : 'nickels', 1 : 'pennies'} payout = payout * 100 denominations = {}
for change in coins.keys(): if change <= payout: amt = payout // change payout = payout % change denominations[coins[change]] = amt
print (denominations)
|
|