Python基于文本的游戏:使用while-Loop和Try-Except猜测游戏

时间:2014-04-26 04:41:15

标签: python python-2.7 while-loop try-except

我正在编写一个简单的基于文本的游戏来增强我对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.")

1 个答案:

答案 0 :(得分: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

如您所见,一旦我们将随机数分配给xx始终保持不变。但是,如果我们继续调用randint(),它就会更改。

回答第二个问题,你不应该在tries之后加{1}},如果值是一个整数,它也会在尝试中加1。相反,请尝试将您的代码合并到以下内容中:

int(raw_input())

您收到错误,因为您错误地回答了所有3次。因此,>>> 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是否为某种东西,而不是:{/ p>

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

修改

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

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

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

相关问题