Python 当用户输入不正确时,如何从指定点重复代码
我是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
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!")
可能重复的