The problem is best explained by just showing the code:
a = True
b = True
while True:
"""
A
"""
if a == True:
pass
"""
B
"""
elif b == True:
pass
The issue being that there is a syntax error at "elif b", though when removing the comments, the issue disappears. I tried removing the indents on the comments which resulted in an expected indent on the closing comment line after "A". I know I could switch to using "#" to comment sections, though """ makes things much clearer and is more convenient for large chunks. Perhaps I'm missing something obvious, I would appreciate any help.
String literals aren't comments. You can sometimes sort of pretend they're comments, but they're not, and the fact that they're not is finally biting you.
An elif
has to appear immediately after the end of the block associated with the preceding if
or elif
. There can be comments and whitespace in between, but no statements, and strings count. Use real comments, with #
.
If you really want to keep pretending strings are comments, you can indent the B string into the body of the if
, but it won't line up cleanly with the block it's intended to be a comment on, and you'll just keep having to mess with your formatting to patch up the differences between comments and string literals.
You're creating a new string when you use """triple quotes"""
. So, you essentially have an un-indented block of code before your elif, which requires a preceding if statement. The improper tabbing on the quotes ends your if
block. Once your parser reaches the elif
block, it doesn't have a matching if block, hence the error.
Triplequotes are used as docstrings in places, and can act like comments, but they're not actually comments.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.