【发布时间】:2016-05-01 04:07:48
【问题描述】:
我创建了一个名为“猜谜游戏”的游戏。故事是:计算机生成一个数字,用户有 3 次机会猜测计算机生成的数字。编码很好也很容易,但我想添加一个小细节,这就是我遇到问题的地方。
我向你们提出的要求是帮助我了解如何/向代码中添加什么内容,以便它可以将新输入与前一个输入相关联,并据此计算打印响应。
import random
listte = range(1, 21) # list contains 1-20
number = random.choice(listte) # computer generated number from the list
for i in range(3):
answer = int(input("What is your guess?"))
prevanswer = None # I came up with this but not really working?
if answer == number:
print "OH YEAH you got it!"
elif answer < number:
print "make it higher"
# this elif is not working with below codes
elif answer < number and answer > prevanswer:
print "still higher"
elif answer > number:
print "lower!"
prevanswer = i # also not working but this is what I came up with
一个示例执行场景:
computer generated : 15
guess 1 : 17
prints "lower!"
guess 2: 10
print "make it higher"
guess 3: 12
print "still higher"
额外:如果您添加一个代码,当用户使用所有 3 猜测时,它会显示“您别无选择”。
【问题讨论】:
-
你的尝试都没有意义,我希望你能看到......