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

cesar gaete
cesar gaete
1,356 Points

my code runs fine in the Teminal(mac) when tested. Can someone please tell me why I keep getting a "Try again", thanks.

I have run the code several times and I do get the Dictionary created with the correct keys and the correct values for each key. When I test the code (I use SUBLIME text editor and the Mac Terminal to test it) I change the return (counts) for print(counts) in order to display the dictionary and see the results, which every time It runs just fine. Can not continue forward since the web page does not let me pass this challenge. I do not see if there is something wrong with the code, please someone help me on this one, thanks Ceasar

wordcount.py
phrase= "I love paris in the winter and in the summer I love paris"

def word_count(words):
    words = phrase.lower().plit()
    counts = {}
    for word in words:
        if word not in counts:
            counts[word] = 0
        counts[word] += 1
    return (counts)

word_count(phrase)

1 Answer

Johannes Scribante
Johannes Scribante
19,175 Points

Hi Cesar,

Your code is correct, except for small changes.

def word_count(words):
    words = words.lower().split()  # note that phrase changed to words and .plit() to .split
    counts = {}
    for word in words:
        if word not in counts:
            counts[word] = 0
        counts[word] += 1
    return counts   # also no need for brackets around the counts

So the first change, when testing you code the function always used phrase and not the words being passed to it. The other comments are just small things. Spelling mistake with split which I am sure you figured out, otherwise your code wouldn't run. And the last one, Python knows the first thing after the return is what is being returned, no need for brackets there.

Otherwise, well done and a cool way to do it, I did it a little different, but I like yours more! Nice!