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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With