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

Hi I got the expected return in workspaces but not in the challenge. Please help. ( I used the zip fuction)

def word_count(string):

string = string.lower()
wordlist = string.split()
wordcount = []
worddict = {}

for word in wordlist:
    count = 0
    for char in word:
        count += 1
    wordcount.append(count)    

worddict = dict(zip(wordlist,wordcount))
'''print (wordlist)
print (wordcount)
print (worddict)'''

return worddict
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(string):

    string = string.lower()
    wordlist = string.split()
    wordcount = []
    worddict = {}

    for word in wordlist:
        count = 0
        for char in word:
            count += 1
        wordcount.append(count)    

    worddict = dict(zip(wordlist,wordcount))
    '''print (wordlist)
    print (wordcount)
    print (worddict)'''

    return worddict

1 Answer

Steven Parker
Steven Parker
231,275 Points

You may have misunderstood the instructions. The challenge wants a dictionary that shows how many times that each unique word appears in the string. But this code seems to be building one that shows how many letters are in each word (with no concern for uniqueness).

Check the example input and output shown in the comments.