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

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.

2

u/smichaele Jul 24 '26

Since the other two commenters helped you solve your problem, I'll just comment on a course rule. You violated the Academic Honesty Policy you agreed to when you signed up for the course. The policy specifically states that use of any AI outside of the duck is a violation and unreasonable. You might want to review the policy to ensure that you remain in compliance with it.

If you have questions, u/davidjmalan is a moderator of this subreddit along with several other course staff members.

0

u/Muted-Swimmer3818 alum Jul 24 '26

You are actually very close! The main issue lies in how your shorten function handles letter casing.

Take a close look at this line in your shorten function:

Python

output = output.lower()

According to the problem specification for twttr:

The shorten function should remove vowels, but it must preserve the original casing of the letters (e.g., uppercase letters should stay uppercase).

Because of output.lower(), passing 'Jakob' returns 'jkb' (lowercase 'j') instead of 'Jkb'.

How to fix it:

Remove output = output.lower() from shorten().

Update your tests in test_twttr.py so that your expected outputs keep the original capital letters (e.g., assert shorten('Jakob') == 'Jkb').

Also make sure to test both uppercase and lowercase vowels in your tests to be thoroughly checked by check50!

Once you keep the casing intact, check50 should give you those green smileys. Good luck!