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 :(
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.
Following is a test of the refactored testing out an item costing $44.00.
See if that meets the spirit of your project, and if so, the extraneous print statements used for visual debugging can be removed.