Python Programming: Multiline Comments before an Else statement

ash*_*ana 4 python comments if-statement

I was working with simple if-else statements in Python when a syntax error came up with the following code.

"""
A multi-line comment in Python
"""
if a==b:
    print "Hello World!"

"""
Another multi-line comment in Python
"""
else:
    print "Good Morning!"
Run Code Online (Sandbox Code Playgroud)

This code gives a syntax error at the "else" keyword.

The following code however does not:

"""
A multi-line comment in Python
"""
if a==b:
    print "Hello World!"

#One single line comment
#Another single line comment
else:
    print "Good Morning!"
Run Code Online (Sandbox Code Playgroud)

Could anyone tell me why this happens? Why does the Python interpreter not allow multi-line comments between if-else statements?

Rai*_*ide 6

您在代码中使用了多行字符串。所以你基本上是在写

if a==b:
    print "Hello World!"

"A string"
else:
    print "Good Morning!"
Run Code Online (Sandbox Code Playgroud)

尽管 Guido Van Rossum(Python 的创造者)建议使用多行字符串作为注释,但 PEP8 建议使用多个单行注释作为块。

请参阅:http : //legacy.python.org/dev/peps/pep-0008/#block-comments