while循环替换Else Statement?(Python)

时间:2013-05-02 16:25:56

标签: python if-statement while-loop

好的,这是

  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#! 它应该工作

1 个答案:

答案 0 :(得分:2)

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'>