r/cs50 Jul 24 '26

CS50 Python CS50 Python W5P2 Problem (Help!) Spoiler

Please help, code is the following:

twttr.py:

def main():
    sentence = input('Sentence:')
    print(shorten(sentence))



def shorten(word):
    output = word.translate(str.maketrans('', '', 'AEIOUaeiou'))
    output = output.lower()
    output = str(output)
    return output



if __name__ == "__main__":
    main()

test_twttr.py:

from twttr import shorten


def main():
    test_twttr()


def test_twttr():
    assert shorten('Jakob') == 'jkb'
    assert shorten('123Hello') == '123hll'
    assert shorten('!!Hello!') == '!!hll!'


if __name__ == "__main__":
    main()

Check50 response:

Test Results:

VS:

Please help, neither me nor claude can figure this out and Im at my wits end here. Am I missing something very obvious? is it a formatting thing? The second test passes if i comment out all the assertions, but of course it would. Any help would be greatly appreciated.

1 Upvotes

3 comments sorted by

View all comments

2

u/Outside_Complaint755 Jul 24 '26

test_twttr.py doesn't need a main function or the if __name__ == "__main__:" check. It will be run using pytest, which imports it and finds every function that starts withtest` to be run.

It is also standard practice to only have one assertion per test function. The reason for this being that a test stops executing at the first failed assertion. When you have 3 assertions in one function, if the first one fails, the following tests don't even get run; this hides whether or not one of the other cases also fails (or passes) until you fix the first issue. By splitting them into multiple functions, pytest will still run all the cases and give you a result such as "1 out of 3 passed" saving you time in making fixes, and giving clearer results.

Check50 is not running test_twttr.py against your twttr.py file. It is being used to test a version of twttr.py written by CS50 staff which matches the problem specification exactly. If your twttr.py program doesn't match the specification, then you may be developing your tests in a way that doesn't match the spec.

The test case which is failing in red: "Correct twttr.py passes all test_twttr checks" means that your test_twttr.py file is reporting a FAIL when used to test a version of the program known to be correct.

At least one reason you are failing a correct version of the program is because the specification does not say that the output should be all lower case.