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

I've labbed the code and I'm getting the result it requests. what am I doing wrong?

I've tried this on my pc and the result comes back as expected. is there an extra requirement I've overlooked?

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):
    #convert string into a list broken up by spaces " "
    string = string.lower()
    result = string.split(" ")
    #make empty dictionary
    my_dict = {}
    #make key for counting
    key = 0
    #make check_list
    check_list = []
    #for each word in list see how many times it occurs and add 1 to key
    for word in result:
        #check check_list for word
        if word in check_list:
            continue
        else:
            key = result.count(word)
            my_dict.update({word: key})
            check_list.append(word)

    return my_dict

2 Answers

Wesley Trayer
Wesley Trayer
13,812 Points

Just pasted your code into the challenge and it only needs one change,

result = string.split(" ")

should be:

result = string.split()

I think this is so if there is more than one space between words it removes all white space.

Excellent! thanks that worked.

That makes sense for double spacing, cheers