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

Stuck this "wordcount.py" challenge. I have, what I think to be a successful code, but I get "Bummer: Try again!"

I have my code that works in a different text editor but I cannot seem to figure out what the editor in Treehouse is looking for.

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_arg):
    mydict = {}
    split = string_arg.split()
    for string in split:
        mydict.update({string.lower():string_arg.count(string)})
    return mydict

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

Try the test:

print(word_count("This is not fine"))

Another approach to try:

  • check if string is in the dict, if so, increment count, otherwise add to dict with count 1

Post back if you need more help. Good luck!!!

wilmington
wilmington
9,845 Points

Hey there, I see the workspace for this task is giving a hard time. I was struggling with my code passing it too. I checked you code in a different editor with different strings and it worked just fine. I honestly, can not explain why the treehouse editor is not accepting the code, however, let me share my script, which passed and it is almost similar to yours, except I lowered values in my string earlier before the loop.

def word_count(word): my_list = word.lower().split() my_dict = {} for item in my_list: my_dict[item] = my_list.count(item) return my_dict

Good luck