Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Collections (2016, retired 2019) Dictionaries Word Count

Wajid Mohammed
Wajid Mohammed
5,698 Points

when i run this is workspace is works but here it says Bummer. Any advice please where I am wrong?

Please help me with this code as upon checking work it doesnt like it.

wordcount.py
# E.g. word_count("I do not like it Sam I Am") gets back a dictionary like:
# {'i': 2, 'do': 1, 'it': 1, 'sam': 1, 'like': 1, 'not': 1, 'am': 1}
# Lowercase the string to make it easier.

def word_count(mystring):
    lower_case_string = mystring.lower()
    count_dict = {}
    for value in (lower_case_string.split(" ")):
        if value in count_dict:
            x = count_dict.get(value)
            x = x + 1
            count_dict[value] = x
        else:
            count_dict[value] = 1
    return count_dict

2 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hey Wajid,

Your code looks good :thumbsup: except for one thing. When you click "Check Work," you get the error message "Hmm, didn't get the expected output. Be sure you're lowercasing the string and splitting on all whitespace!" It's the last part of the last sentence that gives the clue. The instructions for the challenge specifically state to "split on all whitespace" but you are splitting on just spaces as indicated with (" ").
Remember... to split on all whitespace, you don't pass anything into the method, so (" ") should just be ().

Tip: Challenges are very specific and extremely picky. So, usually when something works outside of the challenge, it is because something does not exactly match what the instructions are asking. It could be something as small as a missing period or a spelling error in a string... or something like splitting on spaces instead of all whitespace.

Other than that... Great job on the code!! :)

Keep Coding! :dizzy:

Wajid Mohammed
Wajid Mohammed
5,698 Points

Thanks very much for spotting the problem.