2013-07-06 514 views
-8

enter image description hereElif聲明不起作用

我真的不明白我做錯了什麼。 非常感謝您的幫助。

+3

您有一個縮進問題。向左移動'elif' 4個空格。 –

+0

謝謝,它可以工作,但我在輸入後無法輸入任何內容,它會移動兩行。 – FZEROX

+0

-1請使用圖片粘貼** text **。 –

回答

2

您的縮進是錯誤的。左鍵縮進一次,語法錯誤將消失。而

if < Condition Here>: 
    # Do Something 
elif < Condition Here >: 
    # Do Something 

是正確的缺口

if < Condition Here >: 
    # Do Something 
    elif < Condition Here >: 

會給出一個語法錯誤。縮進在Python中很重要。

Python Docs

前導空白(空格和製表符)在邏輯 行的開始是用於計算行的縮進的水平,這在 反過來被用於確定的分組聲明。

+0

這是所有編程語言中最糟糕的「特性」。 –

2

elif聲明不與if說法正確縮進:

if some_condition: 
    #code 
elif some_other_condition: 
    #code 

docs:在邏輯 行的開頭

是空白(空格和製表符)用於計算行的縮進級別,其中 轉用於確定語句的分組。

來自實例docs

>>> x = int(raw_input("Please enter an integer: ")) 
Please enter an integer: 42 
>>> if x < 0: 
...  x = 0 
...  print 'Negative changed to zero' 
... elif x == 0: 
...  print 'Zero' 
... elif x == 1: 
...  print 'Single' 
... else: 
...  print 'More' 
... 
More 

閒置嘗試這樣:

>>> x = 2 
>>> if x == 0: 
    print x 
elif x == 1: 
    print x 
elif x == 2: 
    print x 
else: 
    print 'foo' 


2 
+1

這不能用作滿意的。 – FZEROX

+2

@FZEROX或者你應該說,縮進:D – TerryA

+0

@FZEROX我已經添加了一個IDLE相關示例。 –

0

你應該建立一個分離的Python腳本,例如tmp.py,並把命令的最複雜的序列有:

if something: 
    pass 
else: 
    pass 

保存它,然後使用run tmp運行,如果你在IPythonpython tmp.py如果你在一個shell(命令提示符,bash等)。