so I've been for a couple of hours now trying to do this test and I wanted to produce a very concatenated way to my result using a Tuple and the divmod function, and I finally got to a solution.. But I can't understand why it only prints 1 result instead of multiple.. here is my code:
def tilt(money):
amount = [(1,'dollar'),(0.25,'quarter'),(0.10,'dime'),(0.05,'nickel'),(0.01,'pennie')]
total = {}
for amount_value, amount_name in amount:
if money >= amount_value:
number_amount, money = divmod(money, amount_value)
total[amount_name] = number_amount
return total
print(tilt(10.65))
# result for this is {'dollar': 10.0}
# what I expect is {'dollar': 10, 'quarter': 2, 'dime': 1, 'nickel': 1}
I just cant wrap my head around a solution, and I would like to avoid going in an endless If-Else code to get to this point. I'm sure there must be a simple solution that I'm missing, thanks in advance for your help..
I can't comment; But it's probably down to your indentation. If you put your return statement in line with your for loop - you'll get the response you're looking for.
so that it is as follows:
def tilt(money):
amount = [(1, 'dollar'), (0.25, 'quarter'), (0.10, 'dime'),
(0.05, 'nickel'), (0.01, 'pennie')]
total = {}
for amount_value, amount_name in amount:
if money >= amount_value:
number_amount, money = divmod(money, amount_value)
total[amount_name] = number_amount
return total