我是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!")