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 trialwarren harris
Courses Plus Student 619 PointsWorking in eclipse, but not working again enough to solve the puzzle and continue
If anybody could take a quick look at it. It works perfect in Eclipse at home, but I can't get it to work enough to continue the course. Thanks.
# 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.
dict = {}
def word_count(i):
ind = 1
for wrds in i.split():
if wrds in i.split():
dict[wrds] = ind + 1
else:
dict[wrds] = ind
2 Answers
Robert Richey
Courses Plus Student 16,352 PointsHi Warren,
I added comments to help illustrate what I think needs attention. Some of this is optional to passing the challenge.
# dict is a builtin python class, consider using a different name
# like my_dict and place it inside the function
dict = {}
# consider changing i to something more descriptive that more
# easily represents the kind of data being passed in, like string
def word_count(i):
# I recommend removing this line
ind = 1
# consider changing wrds to wrd, to help avoid confusion
# about what is available each loop iteration
for wrds in i.split():
# I recommend adding the line below, presuming wrds is changed to wrd
# wrd = wrd.lower()
# ideally, we want to see if wrd is already in my_dict or not
if wrds in i.split():
# if it is, increment the current value += 1
dict[wrds] = ind + 1
else:
# if not, assign a value of 1
dict[wrds] = ind
# return my_dict
That's a lot to go over, and these are just suggestions. Please let me know if this was helpful or not.
Kind Regards
Kenneth Love
Treehouse Guest TeacherYour function doesn't ever return
anything.