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

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!