0

While my code works with some inputs, on other occasions it will round when no rounding is needed (.55 to .56 for example) I'm sure this is a very obvious mistake I've made in the code but I just cant wrap my mind around it.

while True:
    cost = float(input("How much will that be? "))
    payment = float(input("How much are you paying? "))
    if payment >= cost:
        break
    print("Insufficient funds")
# end while

change = payment - cost
remainder = (int(change * 100) % 5)
if remainder == 0:
    rounded = (change * 100)/100
elif remainder in [1,2]:
    rounded = ((change * 100)-remainder)/100
elif remainder in [3,4]:
    rounded = ((change * 100)+5-remainder)/100
print("Total cost   Amount paid   Change   Rounded Change")
print("{0:.2f}         {1:.2f}          {2:.2f}         {3:.2f}".format(cost, payment, change, 
rounded))
Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
  • Please provide the numbers you used achieve this behaviour. Makes it easier to reconstruct. – nomansland008 Sep 21 '21 at 07:24
  • " on other occasions " - we won't guess which. Please provide the input that causes a problem and the corresponding expected vs actual output, and an explanation of why your code is supposed to give this output. – Thierry Lathuille Sep 21 '21 at 07:24
  • @nomansland008 an example would be 25.25 being the cost and 25.27 being the paid amount, with the change being 0.02 and the rounded change being output at 0.01 instead of 0. Another is the cost being 20 and the amount paid being 50.05 with an output of change 30.05 and rounded change 30.06 – testfromgod Sep 21 '21 at 07:30

0 Answers0