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

Justin Houghton
Justin Houghton
4,560 Points

Can someone give me a hint? I have a feeling I am close but can't quite figure out what I'm doing wrong.

I am working on this challenge:

https://teamtreehouse.com/library/python-collections-2/dictionaries/word-count

This is my first time asking a question directly from a challenge and I think my code will be attached to this post. If it isn't, I'll make sure to update.

Here is the challenge request:

I need you to make a function named word_count. It should accept a single argument which will be a string. The function needs to return a dictionary. The keys in the dictionary will be each of the words in the string, lowercased. The values will be how many times that particular word appears in the string.

wordcount.py
def word_count (string):    # create a function that accepts a single string argument
    new_string = string.lower()     # lowercase the string and assign it to a new variable
    string_dictionary = {}      # create an empty dictionary
    for word in new_string.split():     # loop through the words in the string(that is split)
        if word in string_dictionary:   # if a word is already in the string dictionary 
            string_dictionary[word] += 1    # +1 to the value of that key (which is that word)
        else:   # otherwise 
            string_dictionary[word] = 1     # add new key (which is that word) with the value of 1
    return string_dictionary    # return the dictionary

1 Answer

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Justin,

I copy-pasted your code into the challenge and it passed. Try restarting the challenge and then pasting your code back into it.

Cheers,

Alex