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 Python Collections (Retired) Dictionaries Word Count

Please help me with this syntax code

I'm really having trouble with this please can you help me?

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.
def word_count("I am that I am"):
  dict = {}
  my_list = "I am that I am".split()
  for item in my_list:
    dict[item]
    if item in my_list:
      dict[item] += 1
    else:
      dict[item] = 1
    return dict

1 Answer

Zubeyr Aciksari
Zubeyr Aciksari
21,074 Points

Hi Andy, since it is telling us that word_count() takes a string as a value, it automatically assumes the value inside it a string so you don't have to put there a real string like: "def word_count("I am that I am"):"

& Since we working with the dictionary, you don't have to create a new list.

I hope this helps:

def word_count(string_a): dict_a = {} for word in string_a.split(): if word in dict_a: dict_a[word] += 1 else: dict_a[word] = 1 return dict_a

Ahh thanks mate, I've sorted it now:)