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

frank sebring
frank sebring
1,037 Points

correct results but the program says to try again.

I have written the program and the results that it generates are correct. I used the get method which is show in the Python for Everyone class. I am not sure if this method will not work with the grader on treehouse or if there is something else wrong. If anyone has an idea could you please point me in the correct direction. Thank you!!

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(user_input):
    user_statement = str(user_input)
    user_statement = user_input.lower()
    words = user_statement.split()

    statement = dict()
    for word in words:
        statement[word] = statement.get(word, 0) + 1

    print(statement)

word_count("I do not lie it Sam I Am")

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! You are correct. Your code works fine, but you missed a detail in the instructions. You're supposed to return the dictionary, but your code prints the dictionary. Also, you are not meant to call the function yourself.

If I erase the call to the function and return statement instead of print(statement), your code passes!

Hope this helps! :sparkles: