Skip to content
Advertisement

Python : Amount input with While

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))

Advertisement

Answer

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))
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement