Python - 没有特殊原因的语法错误

时间:2017-06-11 11:11:59

标签: python-2.7

我一直在尝试运行这些代码块,但它一直没有给出语法错误

var1 = 100 
if var1: print "1 - Got a true expression value" 
print var1
else:
print "1 - Got a false expression value"
print var1

var2 = 0 
if var2: print "2 - Got a true expression value" 
print var2
else:
print "2 - Got a false expression value"
print var2

print "Good bye!"

1 个答案:

答案 0 :(得分:0)

问题在于你的缩进可能。 这是缩进代码 -

     var1 = 100 
     if var1:
        print "1 - Got a true expression value" 
        print var1 
     else:
        print "1 - Got a false expression value"
        print var1

     var2 = 0 
     if var2:
        print "2 - Got a true expression value" 
        print var2 
     else:
        print "2 - Got a false expression value"
        print var2

     print "Good bye!"

输出:

   1 - Got a true expression value
   100
   2 - Got a false expression value
   0
   Good bye!

Refer here