2014-02-07 144 views
-1

我剛剛在昨天剛剛啓動了python 3,遇到了這個錯誤,我不知道如何解決,請有人幫忙。謝謝! 編輯:它不是縮進,對不起,這裏的代碼可能已經結束了錯誤的縮進,當我把它粘貼在這裏,不得不做4間距。NameError:未定義名稱'q2'

q1 = input("Is it currently raining? ") 
if q1 == "Yes": 
    print("You should take the bus.") 
elif q1 == "No": 
    q2=int(input('How far in km do you need to travel? ')) 

if q2 > 10: 
    print("You should take the bus.") 
elif q2 >= 2 and q2 <= 10: 
    print("You should ride your bike.") 
elif q2 == 1: 
    print("You should walk.") 

錯誤:

Traceback (most recent call last): 
    File "program.py", line 8, in <module> 
    if q2 > 10: 
NameError: name 'q2' is not defined 
+2

請檢查壓痕。 – bereal

+0

你確定你正在運行的Python 3,看起來你是在2.X –

+0

測試你可以通過改變輸入的raw_input來嘗試,因爲輸入在2.x的差異的意義和3 –

回答

2

你壓痕意味着q2正在檢查即使q1 == "Yes",嘗試:

q1 = input("Is it currently raining? ") 
if q1.lower() in ("yes", "y"): # more flexible input 
    print("You should take the bus.") 
elif q1 == "No": 
    q2 = int(input('How far in km do you need to travel? ')) 
    if q2 > 10: # inside elif block 
     print("You should take the bus.") 
    elif q2 >= 2: # already know it's 10 or under 
     print("You should ride your bike.") 
    elif q2 == 1: 
     print("You should walk.")