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

Letha Burchard
Letha Burchard
1,560 Points

workspace does not recognize my code

Every time I run the code, the workspace says that it does not see the function name, even though it is the first line that I write. 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(in_string):
  my_list = in_string.split()
  for item in my_list:
    out_dict[item] =+ 1
  else:
    out_dict[item] = 1
  return out_dict

3 Answers

Ryan Ruscett
Ryan Ruscett
23,309 Points

That's because what you have is not right. It's failing before it has the chance to recognize the function. So it assumes it's not there.

Ryan Ruscett
Ryan Ruscett
23,309 Points

That's because what you have is not right. It's failing before it has the chance to recognize the function. So it assumes it's not there.

Hi Letha,

That's a really good attempt! I'll try to help guide you to the problem areas without giving away the answer.

The first thing I noticed is that you're not lower casing the string. You can either choose to lower case the string before splitting it or chain it just prior to splitting.

# chaining methods
my_list = in_string.lower().split()

# the string will first be lower cased and then it will be split into a list.

Next up is the for loop. Most of it looks pretty good, but there are three major issues. The first is that the else statement is missing it's partner if. Can't have an else without an if. The second issue is the indentation of the else places it outside the for loop. Python is very particular about indentation being used to determine ownership of code within code blocks.

for item in my_list:
  # I'm inside the loop
# I'm outside the loop

Finally, when incrementing values, the shorthand notation is +=. Perhaps what may help to remember this ordering is that first you want to add the two values and then assign back to the variable.

# longhand
out_dict[item] = out_dict[item] + 1

# shorthand
out_dict[item] += 1

What I've left for you to figure out on your own is what to type for the if statement.

if <TODO> :
  out_dict[item] += 1
else:
  out_dict[item] = 1

Hope this helps,

Cheers

Also, I failed to mention that out_dict needs to be created before it can be used and needs to be created as an empty dictionary prior to the loop.

out_dict = {}