Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (Retired) Dictionaries Word Count

Working 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.

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.
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

Hi 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
STAFF
Kenneth Love
Treehouse Guest Teacher

Your function doesn't ever return anything.