2017-03-31 85 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. !"