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.
JavaScript
x
10
10
1
amount = input("What's the total amount of the bill ? :")
2
value = float(amount)
3
4
while (value < 0):
5
print("Please enter an amount higher than 0$ !")
6
amount = input("What's the total amount of the bill ? :")
7
8
else:
9
print("Total amount of the bill:{0}".format(value))
10
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
JavaScript
1
9
1
value = float(input("What's the total amount of the bill ? :"))
2
3
while (value < 0):
4
print("Please enter an amount higher than 0$ !")
5
value = float(input("What's the total amount of the bill ? :"))
6
7
else:
8
print("Total amount of the bill:{0}".format(value))
9