2017-03-31 125 views
-2

當我運行下面的腳本將顯示
語法錯誤:無效的語法的Python的SyntaxError(else語句)

single = True  

if single = True: 
print "Hey Girl, can I get your number ? " 
else 
print "Bye. !" 
+0

它應該是'如果單==真:',甚至更好'如果單:'... –

+1

@ WillemVanOnsem的anwser是不完整的。其他語句也有語法錯誤。 – revy

+0

@WillemVanOnsem我試過'if single == True:'和'if single:'它仍然給我相同的語法錯誤 –

回答

1

如果單隻需輸入:用來檢查是否單是真並繼續執行。如果single是False,它將移動到else語句。另外,您忘記了放在:else語句前面。

single = True 

if single: 
    print ("Hey Girl, can I get your number ? ") 
else: 
    print ("Bye. !") 
+0

只適用於Python 2.X – revy

+0

@revy:nope,真實性在所有版本python ... –

+0

@revy OP被標記爲Python 2.7 – TemporalWolf

0

需要確保else分支在它前面一個冒號!請記住,Python是非常面向空間的,所以需要保持所有內容都適合Python,以便喜歡你。

single = True  

#Instead of just doing single-space indenting it would behoove you to use regular tabs 
if single == True: #<- Also, also, instead of doing just one equal sign (which is assignment) You should use the equality operator '==' 
    print "Hey Girl, can I get your number ? " 
else: #<- You were missing a colon here 
    print "Bye. !"