今天我想设计一个猜数字游戏,由我设置一组数字列表。玩家需要打印出所有的5个数字才能赢得游戏。不允许重复打印数字。以下是代码:
当我尝试运行它时,看到结果后非常困惑:
当我输入数字4时,它应该打印出"That is not what I want",而不是显示“win”。为什么会出错呢?
def guess():
print "Please input a number smaller than 10, let's see if it is in my plan."
print "You should figure out all the 5 numbers."
n = int(raw_input('>'))
my_list = [1, 3, 5, 7, 9]
your_list = []
count = 0
while count < 5:
if n in my_list:
if n not in your_list:
your_list.append(n)
count = count + 1
print "Good job! You have got %d numbers!" % count
n = int(raw_input('>'))
else:
print "You have already typed that. Input again!"
n = int(raw_input('>'))
else:
print "That is not what I want."
n = int(raw_input('>'))
print "Here you can see my plan:", my_list
print "You are so smart to guess out, you win!"
guess()
当我尝试运行它时,看到结果后非常困惑:
Please input a number smaller than 10, let's see if it is in my plan
You should figure out all the 5 numbers
>1
Good job! You have got 1 numbers
>2
That is not what I want
>3
Good job! You have got 2 numbers
>5
Good job! You have got 3 numbers
>7
Good job! You have got 4 numbers
>4
Here you can see my plan: [1, 3, 5, 7, 9]
You are so smart to guess out, you win!
当我输入数字4时,它应该打印出"That is not what I want",而不是显示“win”。为什么会出错呢?
count == 5
,它仍会要求一个额外的输入,但当计数为4时它并不停止。 - tobias_k