当以下代码出现语法错误时,我正在使用Python中的简单if-else语句。
"""
A multi-line comment in Python
"""
if a==b:
print "Hello World!"
"""
Another multi-line comment in Python
"""
else:
print "Good Morning!"
此代码在“ else”关键字处给出语法错误。
但是,以下代码不会:
"""
A multi-line comment in Python
"""
if a==b:
print "Hello World!"
#One single line comment
#Another single line comment
else:
print "Good Morning!"
谁能告诉我为什么会这样?为什么Python解释器不允许在if-else语句之间使用多行注释?
python大神给出的解决方案
您在代码中使用了多行字符串。所以你基本上是在写
if a==b:
print "Hello World!"
"A string"
else:
print "Good Morning!"
尽管Guido Van Rossum(Python的创建者)suggested to use multiline strings as comments,PEP8建议使用多个单行注释作为块。
参见:http://legacy.python.org/dev/peps/pep-0008/#block-comments