'Unable to input my result as expected - logic error?

I don't seem to understand what's wrong with this code, it's not inputting my result as excpected, I think is wrong with my logic.

def main():
# Ask how many cents the customer is owed
    cents = get_cents()
# calculate the number of quarters to give the customer
    quarters = calculate_quarters(cents)
    if cents >= 0.25:
        cents = cents - quarters
    else:
        quarter = 0
    
# calculate the number of dimes to give the customer
    dimes = calculate_dimes(cents)
    if cents >= 0.10:
        cents = cents - dimes
    else:
        dimes = 0
    
# calculate the number of nickels to give the customer
    nickels = calculate_nickels(cents)
    if cents >= 0.05:
        cents = cents - nickels
    else:
        nickels = 0
    
# calculate the number of pennies to give the customer
    pennies = calculate_pennies(cents)
    if cents >= 0.01:
        cents = cents - pennies
    else:
        pennies = 0
    
# sum coins
    coins = quarters + dimes + nickels + pennies
# print total number of coins to give the customer
    print(f"{coins}")



def get_cents():
    while True:
        cents = float(input("Change owed: "))
        if cents > 0:
            break
    return cents

def calculate_quarters(cents):
    if cents >= 0.25:
        quarters = (cents * 100) // 0.25
        return quarters
    

def calculate_dimes(cents):
    if cents >= 0.10:
        dimes = (cents * 100) // 0.10
        return dimes
    
    
def calculate_nickels(cents):
    if cents >= 0.05:
        nickels = (cents * 100) // 0.05
        return nickels
    

def calculate_pennies(cents):
    if cents >= 0.01:
        pennies = (cents * 100) // 0.01
        return pennies

main()

I'm supposed to be getting output results as follows:

if there's an input of 0.41 it should output 4
if there's an input of 0.01 it should output 1
if there's an input of 0.15 it should output 2

The task is to give change equivalent to a particular amount with the least number of coins.
And the names of the coins are "penny", "nickel", "dime" and "quarter".



Solution 1:[1]

Problems that where in the code:

1- What you call centes is actually dollars. So you either compare the number as it is to 0.25 .. etc, or you multiply it by 100 as you did and compare it to 25 insted.

2- You were suptracting the number of quarters, not the value (number of quarters * 0.25)

3- A lot of not needed if statements.

Check this out:

def get_cents():
    while True:
        cents = float(input("Change owed: "))
        if cents > 0:
            break
    return cents

def calculate_quarters(cents):
        return cents // 0.25

def calculate_dimes(cents):
    return cents // 0.10
    
def calculate_nickels(cents):
        return cents // 0.05
    
def calculate_pennies(cents):
        return cents // 0.01


def main():
# Ask how many cents the customer is owed
    cents = get_cents()
# calculate the number of quarters to give the customer
    quarters = calculate_quarters(cents)
    cents = cents - quarters*0.25
    
# calculate the number of dimes to give the customer
    dimes = calculate_dimes(cents)
    cents = cents - dimes*0.1
    
# calculate the number of nickels to give the customer
    nickels = calculate_nickels(cents)
    cents = cents - nickels*0.05

# calculate the number of pennies to give the customer
    pennies = calculate_pennies(cents)
    cents = cents - pennies*0.01

    coins = quarters + dimes + nickels + pennies
    return coins

Edit:

So apparently as mentioned in the comments the accuracy of floats is not good enough here. So for the last pennies the actual value in the cents will be 0.009999999999 which is less than a 0.01 and will not produce a penny. Lets go with the other way which is to multiply by a 100.

Check this out:

import numpy as np
def get_cents():
    while True:
        cents = float(input("Change owed: "))
        if cents > 0:
            break
    return cents

def calculate_quarters(cents):
        return cents // 25

def calculate_dimes(cents):
    return cents // 10
    
def calculate_nickels(cents):
        return cents // 5
    
def calculate_pennies(cents):
        return cents // 1


def main():
# Ask how many cents the customer is owed
    cents = get_cents() * 100
# calculate the number of quarters to give the customer
    quarters = calculate_quarters(cents)
    cents = cents - quarters*25
    
# calculate the number of dimes to give the customer
    dimes = calculate_dimes(cents)
    cents = cents - dimes*10
    
# calculate the number of nickels to give the customer
    nickels = calculate_nickels(cents)
    cents = cents - nickels*5

# calculate the number of pennies to give the customer
    pennies = calculate_pennies(cents)
    cents = cents - pennies*1

    coins = quarters + dimes + nickels + pennies
    return coins

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1