我才刚刚开始学习编程,关注 http://learnpythonthehardway.org . 在学习了循环和 if 语句之后,我想尝试制作一个简单的猜谜游戏。
问题是:
如果你猜错了,它就会卡住,并一直重复“TOO HIGH”或“TOO LOW”,直到你按下 crtl C。
我读过 while 循环,也读过其他人的代码,但我不想只复制代码。
print ''' This is the guessing game!
A random number will be selected from 1 to 10.
It is your objective to guess the number!'''
import random
random_number = random.randrange(1, 10)
guess = input("What could it be? > ")
correct = False
while not correct:
if guess == random_number:
print "CONGRATS YOU GOT IT"
correct = True
elif guess > random_number:
print "TOO HIGH"
elif guess < random_number:
print "TOO LOW"
else:
print "Try something else"
请您参考如下方法:
你必须再次询问用户。
在末尾添加这一行(缩进四个空格以使其保持在 while block 内):
guess = input("What could it be? > ")
这只是一个快速破解。否则我会遵循@furins 提出的改进。
