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

It says some words are missing

It says words are missing but I don't understand why

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.

a_string = "{i} {will} {be} {great}"

string_dict = a_string.split()

def word_count(a_string):
  string_dict = {}

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

2 Answers

Iain Diamond
Iain Diamond
29,379 Points

You have an indentation problem somewhere!

Tip: It think it's a good idea to do some testing on your local machine or in a workspace to debug, as well as play around and learn how the code works.

Fixed it, but now it says [AttributeError: 'tuple' object has no attribute 'split']

Iain Diamond
Iain Diamond
29,379 Points

I'm puzzled as to what you did to 'fix' it. As I see it all that was needed was to tab the else statement so that it matched up with the if.

If this didn't work for you please let me know.