0

我正在编写一个简单的基于文本的游戏来增强我对 Python 的了解。在游戏的一个阶段,用户需要猜测集合 [1, 5] 中的一个数字,以实现他或她的愿望。他们只尝试了三次。我有两个问题:

1) 假设genie_number随机选择为 3。用户每次猜测后,该值是否会发生变化?我不希望程序在每次猜测后随机选择另一个整数。它应该保持不变,因此用户有 3/5 的机会正确猜测它。

2) 我想惩罚不只猜测整数的用户,我已经在本except ValueError节中做到了。但是,如果用户连续进行三个非整数猜测并用尽所有尝试,我希望循环重新定向到else: dead("The genie turns you into a frog."). 现在它给了我下面的错误信息。我该如何解决?

'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.")
4

1 回答 1

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(),它会改变。

回答你的第二个问题,你不应该tries在后面加 1 int(raw_input()),如果值是一个整数,它也会在尝试中加 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
>>> 

您收到错误消息,因为您 3 次都答错了。因此,没有任何内容分配给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
>>> 

编辑

@LosFrijoles关于范围问题的说法相反,错误实际上是由于缺少正确的输入:

>>> 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循环和常规 shell 中,因此这不是范围问题:

>>> 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变得有效。

于 2014-04-26T04:49:34.033 回答