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 trialHoma Shayan
2,038 Pointsdon't understand the error
I have word_count() in my code. I don't understand why the error says 'where's word_count()".
# 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(my_str):
my_str = my_str.lower()
my_list = my_str.split()
for word in my_list:
if word in my_dict:
my_dict[word] += 1
else:
my_dict[word] = 1
return my_dict
1 Answer
Andreas cormack
Python Web Development Techdegree Graduate 33,011 PointsHi Homa
The reason you are getting that error is because there is an error in the script. my_dict is not declared as a dictionary.Try the code below and notice line 4.
def word_count(my_str):
my_str = my_str.lower()
my_list = my_str.split()
my_dict={}
for word in my_list:
if word in my_dict:
my_dict[word] += 1
else:
my_dict[word] = 1
return my_dict
Homa Shayan
2,038 PointsHoma Shayan
2,038 PointsI just defined my_dicts as you suggested (which i did forget) but it didn't fix the problem. It still says "where's is word_count()"
Andreas cormack
Python Web Development Techdegree Graduate 33,011 PointsAndreas cormack
Python Web Development Techdegree Graduate 33,011 PointsHi Homa
I just copy pasted the code from above in the code challenge and it passed.
Homa Shayan
2,038 PointsHoma Shayan
2,038 PointsHi Andres
I had my_dicts instead of my_dict. It worked thank you :)