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

Y. Kravets
Y. Kravets
10,350 Points

Keep getting: 'Some of the words seem to be missing!'

Hi guys!

Admittedly still trying to get myself comfortable with dictionaries and its not going really well. I could use your opinion on what is it I am doing wrong with this code. Thanks!

def word_count(my_st):
  my_dict = {}

  for item in my_st:
    if item in my_dict:
      my_dict[item] += 1
      my_dict[item] = 1
  return my_dict
# 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.

4 Answers

Andre' Jones
Andre' Jones
26,671 Points

the split method works excatly as you explained, but you didnt assign it back to the variable so once you get to the for loop my_string is still = "I love treehouse".

This is what you should do;

my_str = my_str.split()

Andre' Jones
Andre' Jones
26,671 Points

Your problem is the split.

I'll give you a hint but not the Answer so you can think through it.

this is the result your getting now {'a': 3, ' ': 4, 'I': 2, 'h': 1, 'm': 2, 't': 2}

Hint: A string is an array of characters

Y. Kravets
Y. Kravets
10,350 Points

Hi Andre! Thanks for your answer, as a clarification though: to my understanding of the task I need to separate words out of the given string first. I thought that if for example:

my_string = "I love treehouse" my_string.split() would give me then: ["I", "love", "treehouse"], which is exactly what I need because then I go item by item (so in this case word by word).

Am I wrong somewhere?

Y. Kravets
Y. Kravets
10,350 Points

For some unexplainable reason I led myself to believe that once I do my_str.split() it automatically updates my_str to be the output of that. Well, didn't think it through carefully enough. Thank you for pointing it out Andre!