Python 如何循环一行代码,直到用户键入正确答案?
我是一个初学者,我不明白如何循环一行,直到用户键入正确的答案,然后它中断。例如Python 如何循环一行代码,直到用户键入正确答案?,python,python-3.x,Python,Python 3.x,我是一个初学者,我不明白如何循环一行,直到用户键入正确的答案,然后它中断。例如 ask = int(input("How much rice do you want? ")) price_of_1kg = 50 total = ask*price_of_1kg print("This is the price") print(f"Value: ${total}") pay = int(input("Pay this price
ask = int(input("How much rice do you want? "))
price_of_1kg = 50
total = ask*price_of_1kg
print("This is the price")
print(f"Value: ${total}")
pay = int(input("Pay this price mentioned above "))
while True:
if pay == total:
print("good")
break
if pay != total:
int(input("Hey!! Pay the exact amount "))
break
print("Thank you, visit again")
```
I want to loop the second if statement until the user enters the correct amount
您可以这样做:
ask = int(input("How much rice do you want? "))
price_of_1kg = 50
total = ask*price_of_1kg
print("This is the price")
print(f"Value: ${total}")
pay = int(input("Pay this price mentioned above "))
while pay != total:
pay = int(input("Hey!! Pay the exact amount "))
print("Thank you, visit again")
好吧,试着逻辑地思考一下。用你自己的话来说,
break
用于什么?什么时候在你的循环中发生?什么时候不应该发生?