Str.startswith() Not Working As I Intended
Solution 1:
You haven't provided and exit path from the recursive loop. A return statement should do the trick.
(...)
while True:
if line.rstrip().endswith('"""'):
line = infile.readline()
return find_comment(infile, line)
else:
line = infile.readline()
Solution 2:
while True
is an infinite loop. You need to break
once you're done.
Solution 3:
not line_t.startswith('"""') or not line_t.startswith('#')
This expression evaluates to True no matter what string line_t denotes. Do you want 'and' instead of 'or'? Your question isn't clear to me.
Solution 4:
if not line_t.startswith('"""') or not line_t.startswith('#'):
This if
will always be satisfied -- either the line doesn't start with """
, or it doesn't start with #
(or both). You probably meant to use and
where you used or
.
Solution 5:
As long as lines start or end with a comment, the code below should work.
However, keep in mind that the docstrings can start or end in the middle of a line of code.
Also, you'll need to code for triple single-quotes as well as docstrings assigned to variables which aren't really comments.
Does this get you closer to an answer?
defcount_loc(infile):
skipping_comments = False
loc = 0for line in infile:
# Skip one-linersif line.strip().startswith("#"): continue# Toggle multi-line comment finder: on and offif line.strip().startswith('"""'):
skipping_comments = not skipping_comments
if line.strip().endswith('"""'):
skipping_comments = not skipping_comments
continueif skipping_comments: continueprint line,
Post a Comment for "Str.startswith() Not Working As I Intended"