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 (2016, retired 2019) Dictionaries Word Count

I can't seem to figure this one out.

I don't know what is wrong.

wordcount.py
def word_count(string):
  list_of_words=string.lower().split("")
  my_dict={}
  count = 0
  for word in list_of_words:
    if word in my_dict:
      count = count + int(len(word)/len(word))
      my_dict.update({word:count})
      continue
    else:
      my_dict.update({word:1})
  return(my_dict, end = "")    
word_count("I do not like it Sam I Am")

1 Answer

Pete P
Pete P
7,613 Points

I see a couple of issues here.

.1. You should remove the quotes from .split("") like this:

# Using .split() without quotes allows for all types of whitespace to be removed.
list_of_words=string.lower().split()

.2. The way your count variable is set up is incorrect. You're trying to have one 'count' variable track all of the word counts. Try your code in Workspaces with "A A A A word word" as your input string to get a better idea of what's happening. I would eliminate the count variable altogether and do something like this:

if word in my_dict:
# Since the values of the dictionary are ints we can just add 1 each time it's found
        my_dict[word] += 1 

Hope this helps! Let me know if you're still having trouble.