Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.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 - Fatal编程技术网

Python 当用户输入不正确时,如何从指定点重复代码

Python 当用户输入不正确时,如何从指定点重复代码,python,Python,我是Python新手,所以我对基础知识不太确定,所以我理解这可能是一个愚蠢的问题 我在编写代码,用户输入一个数字,如果他们搞错了,我希望代码重新启动,让他们再猜一次,直到猜对为止。我该怎么做 print("Let's play a game! Type in a number and see if you guess mine correctly!") num = input("Type in a number: ") for x in range (0,1): if num == "7

我是Python新手,所以我对基础知识不太确定,所以我理解这可能是一个愚蠢的问题

我在编写代码,用户输入一个数字,如果他们搞错了,我希望代码重新启动,让他们再猜一次,直到猜对为止。我该怎么做

print("Let's play a game! Type in a number and see if you guess mine correctly!")
num = input("Type in a number: ")
for x in range (0,1):
    if num == "7":
        print("Correct!")
    else:
        print("Nope, try again!")

(我也知道很多代码可能是错误的。)

您可以将其放入while循环:

verified = None
while verified is None:
    num = input("Type in a number: ")
    if num == "7":
        print("Correct!")
        # if answer is good
        verified = True
    else:
        print("Nope, try again!")
可能重复的