2013-05-02 189 views
0

確定這裏是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#! 它應該工作

+1

嘗試的raw_input'()'代替。 – 2013-05-02 16:27:06

回答

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