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

word_dict

def word_count(arg)
    word_dict = {}
    word_dict[arg] = count 
    if arg in dict:
      count += 1

# my code is starting to confuse me now!!

edited by mod

2 Answers

Hi Thomas

Welcome to Treehouse.

def word_count(arg)
    word_dict = {}
    word_dict[arg] = count # not sure what is this count meant to represent 
    if arg in dict: # here your checking if the entire string is in the dictionary rather than each word
      count += 1

# see below my code

def word_count(arg):
  word_dict = {}
  words  = arg.split() # split the string into individual words
  for word in words:  # iterate through the list
    if word in word_dict: # if the word is already in the dictionary add 1 to the value
      word_dict[word]= word_dict[word] + 1
    else:
      word_dict[word]=1
  return word_dict 

happy coding

Some where in the dark trenches of my brain a dim light started to stir.