Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Word Count dictionary: I thought my code is correct, but keep getting bummer message.

Here is what I have:

def word_count(string):
    words_dict = {}
    string = string.lower()
    words = string.split(" ")
    for word in words:
        number_of_times = words.count(word)
        words_dict[word] = number_of_times
    return words_dict

Can someone help me?

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(string):
    words_dict = {}
    string = string.lower()
    words = string.split(" ")
    for word in words:
        number_of_times = words.count(word)
        words_dict[word] = number_of_times
    return words_dict

2 Answers

Calling .split(" ") only splits on spaces. The challenge expects you to split on all whitespace. split by default splits on all whitespace if you don't pass it any arguments, therefore you needn't (and shouldn't) pass it any arguments.

words = string.split(" ")  # Works, but not what the challenge wants
words = string.split()  # Pass! :)
Philip Schultz
Philip Schultz
11,343 Points

Hey, just remove the quotations in the split method......then it works