Python文本游戏:使用while循环和try-except的猜数字游戏
我正在写一个简单的文字游戏,目的是提高我对Python的理解。在游戏的一个阶段,用户需要在1到5之间猜一个数字,才能实现他们的愿望。他们只有三次机会。我有两个问题:
1) 假设genie_number
随机选定为3。这个值在用户每次猜测后会改变吗?我不想让程序在每次猜测后随机选择另一个数字。它应该保持不变,这样用户就有3/5的机会猜对。
2) 我想惩罚那些没有猜整数的用户,我在except ValueError
部分做了这个。但如果用户连续三次猜非整数并用完所有机会,我希望循环能跳转到else: dead("精灵把你变成了一只青蛙。")
。现在它给我下面的错误信息。我该怎么解决这个问题?
'Before I grant your first wish,' says the genie, 'you must answer this
'I am thinking of a discrete integer contained in the set [1, 5]. You ha
(That isn't much of a riddle, but you'd better do what he says.)
What is your guess? > what
That is not an option. Tries remaining: 2
What is your guess? > what
That is not an option. Tries remaining: 1
What is your guess? > what
That is not an option. Tries remaining: 0
Traceback (most recent call last):
File "ex36.py", line 76, in <module>
start()
File "ex36.py", line 68, in start
lamp()
File "ex36.py", line 48, in lamp
rub()
File "ex36.py", line 38, in rub
wish_1_riddle()
File "ex36.py", line 30, in wish_1_riddle
if guess == genie_number:
UnboundLocalError: local variable 'guess' referenced before assignment
这是我目前的代码:
def wish_1_riddle():
print "\n'Before I grant your first wish,' says the genie, 'you must answer this riddle!'"
print "'I am thinking of a discrete integer contained in the set [1, 5]. You have three tries.'"
print "(That isn't much of a riddle, but you'd better do what he says.)"
genie_number = randint(1, 5)
tries = 0
tries_remaining = 3
while tries < 3:
try:
guess = int(raw_input("What is your guess? > "))
tries += 1
tries_remaining -= 1
if guess == genie_number:
print "Correct!"
wish_1_grant()
else:
print "Incorrect! Tries remaining: %d" % tries_remaining
continue
except ValueError:
tries += 1
tries_remaining -= 1
print "That is not an option. The genie penalizes you a try. Be careful!"
print "Tries remaining: %d" % tries_remaining
if guess == genie_number:
wish_1_grant()
else:
dead("The genie turns you into a frog.")
1 个回答
回答你的第一个问题,不会。如果你一直调用 randint(1, 5)
,结果会变化,但一旦你把这个随机数赋值给某个变量,它的值就固定了:
>>> import random
>>> x = random.randint(1, 10)
>>> x
8
>>> x
8
>>> x
8
>>> random.randint(1, 10)
4
>>> random.randint(1, 10)
8
>>> random.randint(1, 10)
10
正如你所看到的,一旦我们把随机数赋值给 x
,x
就一直保持不变。不过,如果我们继续调用 randint()
,结果就会变化。
回答你的第二个问题,你不应该在 int(raw_input())
后面直接给 tries
加 1,如果这个值是整数,它也会给 tries
加 1。相反,试着把你的代码整合成下面这样的形式:
>>> tries = 0
>>> while tries < 3:
... try:
... x = raw_input('Hello: ')
... x = int(x)
... except ValueError:
... tries+=1
...
Hello: hello
Hello: 1
Hello: 4
Hello: bye
Hello: cool
>>>
你遇到错误是因为你三次都回答错了。因此,guess
没有被赋值。在你的 while
循环后,你试图检查 guess
是否有值,但实际上它没有:
>>> tries = 0
>>> while tries < 3:
... try:
... guess = int(raw_input('Enter input: '))
... print guess
... except ValueError:
... tries+=1
...
Enter input: hello
Enter input: bye
Enter input: good morning
>>> guess
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'guess' is not defined
不过,如果你输入正确,guess
就会有值:
>>> tries = 0
>>> while tries < 3:
... try:
... guess = int(raw_input('Enter input: '))
... print guess
... except ValueError:
... tries+=1
...
Enter input: 4
4
Enter input: hello
Enter input: 9
9
Enter input: bye
Enter input: 6
6
Enter input: greetings
>>> guess
6
>>>
编辑
>>> x
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined
>>> for k in range(1):
... x = 1
... print x
...
1
>>> x
1
>>>
正如你所看到的,变量 x
在 for
循环和普通的环境中都存在,所以这不是作用域问题:
>>> x
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined
>>> for k in range(1, 3):
... try:
... x = int(raw_input('Hello: '))
... print x
... except ValueError:
... pass
...
Hello: hello
Hello: bye
>>> x
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined
>>>
如你所见,这确实是个错误... :) 由于这个错误,x
永远不会被赋值,除非我们实际输入一个整数,因为代码从未到达 print x
,因为它因为错误而中断:
>>> x
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined
>>> for k in range(1, 3):
... try:
... x = int(raw_input('Hello: '))
... print x
... except ValueError:
... pass
...
Hello: hello
Hello: 8
8
>>> x
8
>>>
当我们确实输入一个整数时,x
就变得有效了。