[英]Python Text-Based Game: Guessing Game Using While-Loop and Try-Except
I'm writing a simple text-based game to enhance my knowledge of Python. 我正在编写一个简单的基于文本的游戏,以增强我对Python的了解。 In one stage of the game, the user needs to guess a number in the set [1, 5] in order to have his or her wish granted.
在游戏的一个阶段,用户需要猜测集合[1,5]中的数字才能实现他或她的愿望。 They only get three attempts.
他们只有三次尝试。 I have two questions:
我有两个问题:
1) Lets assume that genie_number
is randomly selected to be 3. Does this value change after each guess by the user? 1)假设
genie_number
被随机选择为3。在用户每次猜测之后,此值会更改吗? I don't want the program to randomly choose another integer after each guess. 我不希望程序在每次猜测后随机选择另一个整数。 It should remain the same so the user has a 3/5 chance of guessing it correctly.
它应该保持不变,以便用户有3/5的机会正确猜出它。
2) I want to penalize users for not guessing only an integer, and I've done that under the except ValueError
section. 2)我想惩罚用户不要仅仅猜测整数,我已经在
except ValueError
部分except ValueError
做到了。 But if the user makes three non-integer guesses in a row and exhausts all their attempts, I want the loop to re-direct to else: dead("The genie turns you into a frog.")
. 但是,如果用户连续进行三个非整数的猜测并用尽了所有的尝试,我希望循环重新定向到
else: dead("The genie turns you into a frog.")
。 Right now it gives me the error message below. 现在,它给我下面的错误信息。 How do I fix this?
我该如何解决?
'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
Here is my code so far: 到目前为止,这是我的代码:
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.")
Answering your first question, no. 回答您的第一个问题,不。 If you keep calling
randint(1, 5)
, yes it will change, but once you assign it, the value is fixed: 如果继续调用
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
As you can see, once we assign the random number to x
, x
always stays the same. 如您所见,一旦我们将随机数分配给
x
, x
始终保持不变。 However, if we keep calling the randint()
, it changes. 但是,如果我们继续调用
randint()
,它将改变。
Answering your second question, you should not add 1 to tries
right after the int(raw_input())
, if the value is an integer, it will also addd 1 to tries. 回答您的第二个问题,您不应该在
int(raw_input())
之后立即添加1到tries
,如果该值为整数,则尝试次数也将添加1。 Instead, try to incorporate your code into something like below: 相反,请尝试将您的代码合并到如下所示的内容中:
>>> 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
>>>
You are getting the error, because you have incorrectly answered all 3 of the times. 您正在得到错误,因为您在所有3次中均未正确回答。 Therefore, nothing is assigned to
guess
. 因此,没有什么可
guess
。 After your while
loop, you try to see if guess
is something, which it isn't: 在
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
However, if you give correct input, guess
becomes something: 但是,如果您提供正确的输入,
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
>>>
Contrary to what @LosFrijoles about the problem being with the scope, the error was actually due to the lack of correct input: 与@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
>>>
As you can see, the variable x
exists in both the for
loop and in the regular shell, so it is not a scope issue: 如您所见,变量
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
>>>
As you can see, it is a error error... :) Because of the error, x
never gets assigned unless we actually give an integer input, because the code never reaches the print x
, because it breaks due to the error: 如您所见,这是一个错误错误... :)由于该错误,除非我们实际给出一个整数输入,否则
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
>>>
When we do give an integer input, x
becomes valid. 当我们给一个整数输入,
x
生效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.