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

Create a function named word_count that takes a string......

I am have been stuck here on this objective for two days trying to figure it out without any help and I have tried everything. Any help is appreciated.

def word_count(a_sting):
  for word in a_string.split():
    if a_string[word]:
      a_string[word] += 1
    else:
      a_string[word] = 1
      return word_count

2 Answers

Hey Shawndee.

The parameter that you have given the function is called a_sting but in the function you have referred to it as a_string.

Have a great day!

-Luke

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

You're only returning in the else and what you're returning is a reference to the function.

def word_count(a_string):
    counts = {}
    for word in a_string.split():
        if word in counts:

Let me start you off with this. This doesn't account for upper and lowercase versions of the same word, though, and doesn't handle incrementing or even setting a count for the word, but you should be able to figure out those bits. At the end, you'll want to return counts.