Home > Net >  Having trouble with while loops and collecting user input
Having trouble with while loops and collecting user input

Time:06-10

Hi I'm working on an assignment where I make a self-service program. I'm having trouble trying to incorporate a while loop while collecting user input. I want my code to collect an amount of money (entered by the user) and prompt the user on how much more they need to pay. Once the amount paid exceeds the amount due, I want to continue with the rest of my code. This is what I have so far but the loop stops after 2 inputs, even if the user has not paid enough.

elif paid < TotalCost:
  due = TotalCost - paid
  while due > 0:
    print("You need to pay $",round(due,2)," more.")
    morepaid=float(input("Enter the amount paid: $"))
    if morepaid>TotalCost:
      change= morepaid- TotalCost
      print("Change due: $",round(change,2))
      print(" ")
      print("Thank you for visiting McDonald's")
  
    elif due== 0:
      print("Change due: $0.00")
      print(" ")
      print("Thank you for visiting McDonald's")
    else:
      due -= morepaid

CodePudding user response:

You can simply subtract the amount paid from due, you don't need another variable totalpaid.

elif paid < totalCost:
    due = totalCost - paid:
    while due > 0:
        print("You need to pay $",round(due,2)," more.")
        morepaid=float(input("Enter the amount paid: $"))
        due -= morepaid
  • Related