Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CS50P Problem Set 6 - Lines of Code #310

Open
HythamTag opened this issue Feb 5, 2025 · 1 comment
Open

CS50P Problem Set 6 - Lines of Code #310

HythamTag opened this issue Feb 5, 2025 · 1 comment

Comments

@HythamTag
Copy link

HythamTag commented Feb 5, 2025

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()
@max-min-median
Copy link

From the problem set specification:
"A docstring should not be considered a comment."

Docstrings should be counted as lines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants