A friend and I coded these two functions to answer a problem of how many coins you would need to give back for change if given the total value of the change. Quarters, dimes, nickels, and pennies:
The magnitude of our change values are giving us different answers, however I was unsure how to explain this difference
def num_coins(cents):
coins = [25, 10, 5, 1]
count = 0
for coin in coins:
while cents >= coin:
cents = cents - coin
count += 1
return count
#########
def coin_return(change):
coin_options = [.25,.10,.05,.01]
number_of_coins = 0
for coin in coin_options:
while change >= coin:
number_of_coins += 1
change = change - coin
return number_of_coins
print(coin_return(.24))
print(num_coins(24))
The right output is six, two dimes and four pennies. The num_coins function returns this, however the coin_return function returns five. What's happening here? Am I missing something obvious?