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 trialnomadnick
7,735 PointsI 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
Jason Anello
Courses Plus Student 94,610 PointsHi 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.
Kenneth Love
Treehouse Guest TeacherYeah, good code, just missing the return
like Jason Anello pointed out.
nomadnick
7,735 PointsThank you Kenneth. Really enjoying the course and looking forward to the object oriented one!
Andrew Merrick
20,151 PointsThanks 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?
Jason Anello
Courses Plus Student 94,610 PointsHi 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
20,151 PointsThanks, 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
Treehouse Guest TeacherYou 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
20,151 PointsThanks for the assist and the update!
nomadnick
7,735 Pointsnomadnick
7,735 PointsAh, so close, yet so far... Thank you Jason!