Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何循环一行代码,直到用户键入正确答案?_Python_Python 3.x - Fatal编程技术网

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
用于什么?什么时候在你的循环中发生?什么时候不应该发生?