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

word_count - trouble with my code

I just keep getting bummer try again. It works on my computer so I'm not sure why it's not working here.

wordcount.py
def word_count(string):
    list_of_words=string.lower().split(" ")
    my_dict={}
    count=0
    for word in list_of_words:
        if word in my_dict:
            my_dict.update({word:count+=1})
        else:
            my_dict.update({word:count = 1})
    print(my_dict)

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

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

As Pete mentioned, the split should be on whitespace (the default with no argument) instead of a literal space

Other errors to fix:

  • the print should be a return
  • when using update, use a value instead of an assignment statement
  • to update an existing value add one to the present value my_dict[word]+1

Since you're accessing the current value in my_dict, a short cut would be to update it directly instead of using update: my_dict[word]+=1

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

Pete P
Pete P
7,613 Points

Try removing the quotes from .split():

list_of_words=string.lower().split()

That way, I believe all types of whitespace will be removed.

Also, I'm guessing the last line of code was included just for testing purposes. It doesn't need to be in your final answer.

Thank you Pete P and Chris. I am finally passed this challenge :)