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 :(
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.