Open
Description
I'm working on the lines.py
problem in CS50P, and I encountered an issue with the check50
tests. When I include logic to skip docstrings (lines starting with """
or '''
), the check50
test fails for the case where the file contains docstrings. However, if I remove the docstring-checking logic, the tests pass.
It gives me the following errors:
:( lines.py
yields 9 given a file with 9 lines, whitespace, comments, and docstrings expected "9", not "1\n".
:| lines.py
yields 2058 given 2058 lines of code in an open-source library file can't check until a frown turns upside down.
Here is my code:
import sys
import os
def main():
if len(sys.argv) != 2:
sys.exit("Too few command-line arguments" if len(sys.argv) < 2 else "Too many command-line arguments")
if not sys.argv[1].endswith(".py"):
sys.exit("Not a python file")
try:
print(count_lines(sys.argv[1]))
except FileNotFoundError:
sys.exit("File does not exist")
def count_lines(filename):
line_count = 0
is_doc_string = False
with open(filename, 'r', encoding="utf-8") as file:
for line in file:
line = line.lstrip()
if not line:
continue
# if line.startswith('"""') or line.startswith("'''"):
# is_doc_string = not is_doc_string
# continue
# if is_doc_string == True:
# continue
# else:
if not line.startswith("#"):
line_count += 1
return line_count
if __name__ == "__main__":
main()
Metadata
Metadata
Assignees
Labels
No labels