Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Programming: Multiline Comments before an Else 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!"

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!"

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

like image 537
ashishbaghudana Avatar asked Dec 25 '22 23:12

ashishbaghudana


1 Answers

You're using multiline strings in your code. So you're basically writing

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

"A string"
else:
    print "Good Morning!"

Although Guido Van Rossum (the creator of Python) suggested to use multiline strings as comments, PEP8 recommends to use multiple single line comments as block.

See: http://legacy.python.org/dev/peps/pep-0008/#block-comments

like image 71
Railslide Avatar answered May 12 '23 04:05

Railslide