So basically I'm trying to create an elementary python program that takes a float as input (managed by cs50 lib) and then subtracts quarters, dimes, nickels, and pennies and calculates the total amount of coins. Somewhere my float gets messed up and starts returning peculiar values like the input of 0,41 changes to 0.15999999999999998 after 0.25 has been subtracted. Why is this happening?
import cs50
def get_cents():
cents = cs50.get_float("Amount of cents owed: ")
return cents
def calculate_quarters(cents):
quarters = 0
while cents >= 0.25:
cents = cents - 0.25
quarters += 1
return quarters
def calculate_dimes(cents):
dimes = 0
while cents >= 0.10:
cents = cents - 0.10
dimes += 1
return dimes
def calculate_nickels(cents):
nickels=0
while cents >= 0.05:
cents = cents - 0.05
nickels += 1
return nickels
def calculate_pennies(cents):
pennies= 0
while cents >= 0.01:
cents = cents - 0.01
pennies += 1
return pennies
# Ask how many cents the customer is owed
cents = get_cents()
print(cents)
# Calculate the number of quarters (25 cent) to give the customer
quarters = calculate_quarters(cents)
cents = cents - (quarters * 0.25)
print(cents)
#Calculate the number of dimes (10 cent) to give the customer
dimes = calculate_dimes(cents)
cents = cents - (dimes * 0.10)
print(cents)
# Calculate the number of nickels (5 cent) to give the customer
nickels = calculate_nickels(cents)
cents = cents - (nickels * 0.05)
print(cents)
# Calculate the number of pennies (1 cent) to give the customer
pennies = calculate_pennies(cents)
cents = cents - (pennies * 0.01)
print(cents)
# Sum coins
coins = (quarters + dimes + nickels + pennies)
print(cents)
# Print total number of coins to give the customer
print(coins)