Python Dollar Change Calculator not giving correct amounts

119 views Asked by At

I am writing a program for my Python class homework. I have a good baseline, however, it's been 3 days and now this problem is just actually a problem and I am stuck.

My professor wants the change to be given with no cents, and to start giving change based on the largest possible dollar bill. No if statements can be used, and it can supposedly be done with just mathematical operators.

I tried searching for help, but everyone is using if statements and I am not sure if this is a math issue or a program issue! The main thing that happens is the 50 dollar bill and 20 dollar bill seem to be giving correct change but then at the 10, 5, and 1 dollar bill it stops working.

I deleted the parts for the 5 and 1 dollar bill but I did try them. The professor also wants it printed out in an f string.

def main():
    cost = int(input('Enter the dollar amount of the item(no cents): '))
    h_bill = int(100)
    dollars_left = h_bill - cost
    fifty_change = (dollars_left // 50)
    fifty_left = dollars_left % 50
    twenty_change = (fifty_left // 20)
    twenty_left = dollars_left % 20
    ten_change = (twenty_left // 10)
    ten_left = dollars_left % 10
    
    print('Your change is: ')
    print(f'{fifty_change:,.0f} Fifty Dollar bill(s)')
    print(f'{twenty_change:,.0f} Twenty Dollar bill(s)')
    print(f'{ten_change:,.0f} Ten Dollar bill(s)')

main()
  • I tried multiple different variables for the change calculations.
  • I tried putting the floor division and remainder division in the same lines.
  • I have tried every variation of dividing the initial cost the user inputs and dividing the change by each bill to receive change but it's just not making sense, sometimes the change is correct at 50 and 20 and then it doesn't give the correct change for the rest. or sometimes it's just all plain wrong :(
3

There are 3 answers

0
lengthylyova On

You declare so many variables that it's easy to get confused. You should think the way you would in life. Programming is one way to automate human tasks, but before doing that, you need to build an algorithm in your head. I'm sure if you start solving this problem on paper, there will be fewer variables.

def calculate_change(paid:int, cost:int) -> int:
    ''' Prints change based on the largest possible dollar bill
    '''
    change = paid - cost # change to give back
    bills_list = [50, 20, 10, 5, 2, 1] # possible dollar bills
    
    print("Your change is:")
    # for every bill value in bills list
    for value in bills_list:
        bills_to_give = change // value # counting how many bill of current value you need to give
        if bills_to_give: # if bills_to_give not equals 0
            # printing how many bill with current value you will give back
            print(f"{change // value} | {value} Dollar bill(s)") 
        change %= value # updating change


def main():
    ''' Main function to launch on program start
    '''
    calculate_change(100, int(input("Enter the dollar amount of the item(no cents):\t")))


if __name__ == "__main__":
    ''' Runs only if the entry point (startup file) was the current file.
    '''
    main()
Enter the dollar amount of the item(no cents):  24
Your change is:
1 | 50 Dollar bill(s)
1 | 20 Dollar bill(s)
1 | 5 Dollar bill(s)
1 | 1 Dollar bill(s)
0
Sid On

I'm not sure why you're creating the 'fifty_left' etc. variables. It seems more intuitive to repeatedly update the 'dollars_left' variable.

def main():
    cost = int(input('Enter the dollar amount of the item(no cents): '))
    h_bill = int(100)
    dollars_left = h_bill - cost
    fifty_change = (dollars_left // 50)
    dollars_left = dollars_left % 50
    twenty_change = (dollars_left // 20)
    dollars_left = dollars_left % 20
    ten_change = (dollars_left // 10)
    dollars_left = dollars_left % 10
    
    print('Your change is: ')
    print(f'{fifty_change:,.0f} Fifty Dollar bill(s)')
    print(f'{twenty_change:,.0f} Twenty Dollar bill(s)')
    print(f'{ten_change:,.0f} Ten Dollar bill(s)')
main()
1
NoDakker On

In testing out your code and adding in some print statements, it was evident that you were repeatedly reusing the "dollars_left" variable in your calculations of twenties, tens, and so forth.

Also, there did not seem to be any accommodation for five dollar bills and/or one dollar bills. As your program works its way down calculating denominations, it needs to progress through which variable is used to derive the next smallest bill quantity.

With that in mind, following is a refactored version of your program, leaving in some extra "print" statements to illustrate what is happening in the calculation of change.

def main():
    cost = int(input('Enter the dollar amount of the item(no cents): '))
    h_bill = int(100)
    dollars_left = h_bill - cost
    print("Dollars left after cost: ", dollars_left)
    fifty_change = (dollars_left // 50)
    fifty_left = dollars_left % 50
    print("fifty_left: ", fifty_left)
    twenty_change = (fifty_left // 20)
    twenty_left = fifty_left % 20
    print("twenty_left: ", twenty_left)
    ten_change = (twenty_left // 10)
    ten_left = twenty_left % 10
    print("ten_left: ", ten_left)
    five_change = (ten_left // 5)
    one_change = ten_left % 5
    print("one_change: ", one_change)
    

    print('Your change is: ')
    print(f'{fifty_change:,.0f} Fifty Dollar bill(s)')
    print(f'{twenty_change:,.0f} Twenty Dollar bill(s)')
    print(f'{ten_change:,.0f} Ten Dollar bill(s)')
    print(f'{five_change:,.0f} Five Dollar bill(s)')
    print(f'{one_change:,.0f} One Dollar bill(s)')
    

main()

Following is a test of the refactored testing out an item costing $44.00.

craig@Vera:~/Python_Programs/Change$ python3 change.py
Enter the dollar amount of the item(no cents): 44
Dollars left after cost:  56
fifty_left:  6
twenty_left:  6
ten_left:  6
one_change:  1
Your change is: 
1 Fifty Dollar bill(s)
0 Twenty Dollar bill(s)
0 Ten Dollar bill(s)
1 Five Dollar bill(s)
1 One Dollar bill(s)

See if that meets the spirit of your project, and if so, the extraneous print statements used for visual debugging can be removed.