-1

Beginner here. I'm trying to build a loop where the chekout displays a total amount that has to be over 0$. For example, if I start with 450$ the code works. But if I start with say -12 it will ask me again (which is what I want), but then if I enter 450 as the third essay; the first condition keeps runing. Why is that? Thanks in advance.

amount = input("What's the total amount of the bill ? :")
value = float(amount)

while (value < 0):
    print("Please enter an amount higher than 0$ !")
    amount = input("What's the total amount of the bill ? :")
    
else:
    print("Total amount of the bill:{0}".format(value))
Bob
  • 3
  • 1
  • 4
    `while..else` is not what you think it is. Also you never update `value` inside the `while` loop. – Selcuk Feb 03 '21 at 06:45
  • 1
    You might want to read: [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Matthias Feb 03 '21 at 07:25

1 Answers1

0

You forgot to update the variable "value" so your while loop condition remains true even after inputting 450 (value is still -12). You can convert the input to a float in the same line too, which removes the need for the "amount" variable

value = float(input("What's the total amount of the bill ? :"))

while (value < 0):
    print("Please enter an amount higher than 0$ !")
    value = float(input("What's the total amount of the bill ? :"))
    
else:
    print("Total amount of the bill:{0}".format(value))
chngzm
  • 608
  • 4
  • 13