Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (2016, retired 2019) Dictionaries Word Count

Andrew Bickham
Andrew Bickham
1,461 Points

def word_count

I keep getting a type error that say "must be str, not list" which i am confused about, if someone could hint at what im doing wrong that would be greatly appreciated

wordcount.py
# E.g. word_count("I do not like it Sam I Am") gets back a dictionary like:
# {'i': 2, 'do': 1, 'it': 1, 'sam': 1, 'like': 1, 'not': 1, 'am': 1}
# Lowercase the string to make it easier.


def word_count(sentence):
     words = sentence.lower().split()
     for word in words:
          result = dict(word.count(words))
          return result

2 Answers

Steven Parker
Steven Parker
216,033 Points

I believe the message is complaining about the string version of "count" being passed a list argument instead of a substring. But even if that is fixed, "count" will return a single int value, which by itself would not be sufficient data to create a new dictionary from.

And a couple more hints:

  • you'll want to build up a dictionary instead of creating a new one for each word
  • the "return" should be moved out of the loop to avoid ending it prematurely on the first pass
Andrew Bickham
Andrew Bickham
1,461 Points
def word_count(sentence):
    word = sentence.lower().split()
    d = {}
    for item in word:
       d[item] = word.count(item)
     return d

okay so i understood the hint where you said i needed to build a dictionary so i added an empty one and build it that way, so if you could give me one or two more pointers, to help crack this code, everytime i try and clear the challenge all it says is "try again"

Andrew Bickham
Andrew Bickham
1,461 Points
def word_count(sentence):
    word = sentence.lower().split()
    d = {}
    for item in word:
       d[item] = word.count(item)
     return d

I made some correction but i still keep getting a bummer message that says, try again, if you hint at what the problem is that would be greatly appreciated it

Steven Parker
Steven Parker
216,033 Points

You've essentially got it now. :+1: The only remaining issue is simply formatting — the "return" statement is indented just a bit too much to line up with the other items at the same indentation level.

Fix that and you'll pass.

Andrew Bickham
Andrew Bickham
1,461 Points

lol as simple as that thank you!!

Steven Parker
Steven Parker
216,033 Points

Andrew Bickham — Glad to help. You can mark a question solved by choosing a "best answer".
And happy coding!