2017-05-17 20 views
-1

我昨天啓動了Python並且練習製作小腳本,但沒有一個最終運行。我有點基本的東西,但我感到非常愚蠢和沮喪。這是其中的一個腳本。'else'的語法錯誤並且找不到問題

UserInput = input("Enter") 
if UserInput == "yes": 
print ("good job") 
elif print ("wrong"): 
else: 
    return 
+4

您發佈的代碼是否正確?縮進是錯誤的,並且縮進在python – EdChum

+5

中很重要,其中「elif」是有條件的嗎? –

+0

'elif'用於檢查另一個條件,'else'在語句結尾使用_once_作爲默認值。 –

回答

-1

檢查壓痕

UserInput = input("Enter") 
if UserInput == "yes": 
    print ("good job") 
else: 
    print ("wrong") 
    return 
+2

那個'elif'情況讓我失望了! – schwobaseggl

+0

您的代碼無效python:'SyntaxError:invalid syntax' –

0

這應該幫助。我並沒有完全得到你想要做的elif部分,所以在這段代碼中,當且僅當輸入爲yes時,它纔會執行if條件,否則將始終執行else條件。

P.S. : - 如果你能澄清你想要的東西,這將是一件好事。

UserInput = raw_input("Enter :- ") 
if UserInput == "yes": 
    print ("good job") 
else: 
    print ("wrong") 
0

elif必須指定一個條件和縮進必須是整個模塊是一致的:

UserInput = input("Enter") 
if UserInput == "yes": 
    print ("good job") # <-+ 
elif True or False:  # | elif needs a condition as well 
    print ("wrong"): # <-+-- indentation must be indentical 
else:     # | 
    return    # <-+ 
4

有3個問題的代碼。第一個是正確的縮進。如果您的代碼不像下面那樣,請使用[tab] *來修復它。對於其他問題,請參閱代碼中的註釋:

UserInput = input("Enter") 
if UserInput == "yes": 
    print ("good job") 
# elif requires a condition to evaluate. It's a shorthand for else if 
# you can do e.g. elif UserInput == "no" 
elif (condition): # colon goes here 
    print ("wrong") 
else: 
    return 

編程需要精確的語法。當你開始編程時,識別這些小的滋擾是困難的。繼續嘗試。它會變成自動的。

*實際上您應該使用4個空格,請參閱Matthias下面的註釋。我個人有我的IDE配置爲當按[tab]時插入4個空格。

+1

縮進應該用空格而不是製表符(參見[Python代碼樣式指南](https://www.python.org/dev/peps/ pep-0008 /#tabs-or-spaces)。 – Matthias

+0

哦該死的:D感謝您指出! – mknull