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 (Retired) Dictionaries Word Count

Whats wrong with my code??

I ran this on my PC and i get the expected results. But when i run the code in this challenge it gives me an error 'Bummer! Some of the words seem to be missing'. I don't see the issue i have done what it is asking?? Please help!

word_count.py
# E.g. word_count("I am that I am") gets back a dictionary like:
# {'i': 2, 'am': 2, 'that': 1}
# Lowercase the string to make it easier.
# Using .split() on the sentence will give you a list of words.
# In a for loop of that list, you'll have a word that you can
# check for inclusion in the dict (with "if word in dict"-style syntax).
# Or add it to the dict with something like word_dict[word] = 1.
def word_count(s):
  s = s.lower()
  my_dict = {'i': 0, 'am':0,'that':0}
  for loop in s.split():
    if loop in my_dict:
      my_dict[loop] += 1
  return(my_dict)





word_count("I am that I am")

2 Answers

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello Thomas.

I see a few things here that can lead to the errors.

First thing is that we only want to define the function, the compiler will take care of the rest, so no need to call the function at the bottom.

Second thing that I notice is that you start with a dictionary that is not empty, your are basically hard-coding it, and I would recommend you do not do this, try starting with an empty dictionary, something like: my_dict = {}

The for loop:

it looks ok to me, but you are missing the case where the word is not in the dictionary already, so I would add an "else" statement, setting the count equal to 1 in this case.

You should then be good to go.

Let me know if you need further help!

PS: I kindly suggest you NOT to try to solve the code challenges in your computer's environment because you need to add extra pieces of code to test your scripts and this often leads to problems when you paste your final output into the code challenge. I think it would be easier and faster for you to work directly in the code challenge environment.

;)

Thanks man! :)