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

Help on getting a word count from a dict.

I am sure this challenge combos several lessons I have done so far but I am having a hard time piecing this one together...

Alright, this one might be a bit challenging but you've been doing great so far, so I'm sure you can manage it. 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. Check the comments below for an example.

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("green eggs and ham"):
    dict = {}

    split_string = ((string.lower()).split())
    for word in split_string:
        word_count = split_string.count(word)
            dict[word] = word_count 
                print(dict)

1 Answer

Aaron Nolan
Aaron Nolan
5,714 Points

Hey Jaclyn! So this is a great answer but you've just made a few simple mistakes which is causing your answer not to pass, I'm going to just comment through your code to explain the problems :

# You put a literal string in here when it should just be a variable name for the argument
# That way when you call the method anything can be put there
def word_count("green eggs and ham"):

    # dict is a type in Python and its bad practice to use it as a variable name
    # something even as simple as a_dict is a better choice
    dict = {}

    # So here you're using string.lower().split() but string as a variable is not defined
    # If you had string above instead of "green eggs and ham" this would make sense
    # And in my opinion you don't need all those extra brackets, technically its fine
    # But i believe string.lower().split() looks cleaner and less chance for a missing bracket
    split_string = ((string.lower()).split())
    for word in split_string:
        word_count = split_string.count(word)
            dict[word] = word_count 
                # Finally here, the question asks you to return the dict, but you're printing it
                # Also the indentation is wrong, it should be level with the for loop
                # So it only runs once
                print(dict)

So I hope this explains the problems well enough, if you need some more clarification feel free to ask. I slightly modified you're code just to show you how it should change to pass the question :

def word_count(a_str):
    a_dict = {}
    split_string = a_str.lower().split()

    for word in split_string:
        word_count = split_string.count(word)
        a_dict[word] = word_count

    return a_dict

Hope this helps and happy coding :)