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

I am running into an issue with the Dictionaries Challenge in Python Collections

I am not sure where I am going wrong on this challenge. I have tried numerous answers but feel like I am on the right track here. Treehouse disagrees, so I am hoping another set of eyes can help see what I am missing?

def word_count(wordstring):
  string_dict = {}
  lower_string = wordstring.lower()
  word_list = lower_string.split()
  for word in word_list:
    if word in string_dict:
      string_dict[word] += 1
    else:
      string_dict[word] = 1

4 Answers

Hi nomadnick,

You're not returning the dictionary. Make sure that it's only indented once so that it's in the function block but not in the for block.

Ah, so close, yet so far... Thank you Jason!

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Yeah, good code, just missing the return like Jason Anello pointed out.

Thank you Kenneth. Really enjoying the course and looking forward to the object oriented one!

Andrew Merrick
Andrew Merrick
20,151 Points

Thanks for the help Jason and Kenneth! I keep getting a Syntax error:

SyntaxError: return my_dict[item] += 1

def word_count(my_string):
  my_dict = {}
  lower_string = my_string.lower()
  split_string = lower_string.split()
  for item in split_string:
    if item in my_dict:
      return my_dict[item] += 1
    else:
      return my_dict[item] = 1

Any help/guidance would be appreciated.

On a side note: any future videos on Django development in the works?

Hi Andrew,

You don't want to have return statements inside the for loop otherwise the for loop, and the function, will end upon encountering the first return statement if it's a valid statement or a syntax error if not.

You want to return my_dict at the end of the function, after the for loop is finished.

Let me know if you're still having trouble.

Andrew Merrick
Andrew Merrick
20,151 Points

Thanks, Jason. I got it fixed. Really appreciate the help from you both. When I put the return at the end of the for loop it passed.

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

You don't want to return inside the for since that'll return data out of the function instead of increasing anything in the dictionary.

Re: Django. Yes, it's on the way. A few more things before we get there, but I'm hoping we'll start with Django before 2015.

Andrew Merrick
Andrew Merrick
20,151 Points

Thanks for the assist and the update!