Python SyntaxError(else语句)

时间:2017-03-31 17:10:48

标签: python python-2.7 if-statement

当我运行下面的脚本时,它会显示出来
SyntaxError:语法无效

single = True    

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

2 个答案:

答案 0 :(得分:1)

简单地输入if single:which检查single是否为True并继续执行。如果single为False,它将移动到else语句。另外,你忘了把:放在else语句前面。

single = True

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

答案 1 :(得分: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. !"