While循环替换Else语句?(Python)的
问题描述:
确定这里是While循环替换Else语句?(Python)的
done = False
while not done:
quit = input ("Do you want to quit? ")
if quit == "y" :
done = True;
if not done:
attack = input ("Does your elf attack the dragon? ")
if attack == "y":
print ("Bad choice, you died.")
done = True;
,但是当我到达
Do you want to quit?
我进入
n
我得到
Traceback (most recent call last):
File "C:\Users\your pc\Desktop\JQuery\dragon.py", line 4, in <module>
quit = input ("Do you want to quit? ")
File "<string>", line 1, in <module>
NameError: name 'n' is not defined
根据此http://www.youtube.com/watch?feature=player_embedded&v=2Z2pH0Ls9Ew#! 它应该工作
答
input
在Python的版本2和3中表现不同。你很清楚Python 2,因为它试图解释Python环境中的输入。
您只需要raw_input()
即可读取字符串。
编辑: 为了使差明确,在Python 2:
>>> type(input())
0
<type 'int'>
>>> type(raw_input())
0
<type 'str'>
在Python 3:
>>> type(input())
0
<class 'str'>
尝试的raw_input'()'代替。 – 2013-05-02 16:27:06