python中的猜谜游戏 [英] Guessing game in python
问题描述
我刚刚开始按照 http://learnpythonthehardway.org 学习编程.在学习了循环和 if 语句之后,我想尝试制作一个简单的猜谜游戏.
I have only just started to learn to program following http://learnpythonthehardway.org. After learning about loops and if-statements I wanted to try to make a simple guessing game.
问题是:
如果您的猜测不正确,它会卡住并不断重复太高"或太低",直到您点击 crtl C.
If you make an incorrect guess it gets stuck and just keeps repeating either "TOO HIGH" or "TOO LOW" until you hit crtl C.
我已经阅读了 while 循环并阅读了其他人的代码,但我只是不想复制代码.
I have read about while loops and have read other peoples code but I simply dont want to just copy the code.
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
块中):
Add this line at the end (indented by four spaces to keep it within the while
block):
guess = input("What could it be? > ")
这只是一个快速的技巧.否则我会遵循@furins 提出的改进.
This is just a quick hack. I would otherwise follow the improvement proposed by @furins.
这篇关于python中的猜谜游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!