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

Donald Tam
Donald Tam
1,570 Points

word count.py

I have written below: and checked on text Wrangler with terminal

seem got the correct answer.


pls help correct me and told me where

Thx thx thx

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.

list=[]

dict={} key = None value = None

def word_count(string): list = string.split()

for i in list:
    key = i.lower()

    if key in dict:

        value = dict[key]+1

        dict.update({key:value})

    else:
        dict.update({key:1})



print(dict)    

word_count("I do not like it Sam I Am")

wordcount.py
I have written below:
and checked on text Wrangler with terminal

seem got the correct answer.

********

pls help correct me and told me where 

Thx thx thx




# 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.
#list=[]
dict={}
key = None
value = None



def word_count(string):
    list = string.split()

    for i in list:
        key = i.lower()

        if key in dict:

            value = dict[key]+1

            dict.update({key:value})

        else:
            dict.update({key:1})



    print(dict)    


word_count("I do not like it Sam I Am")

4 Answers

James J. McCombie
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
James J. McCombie
Python Web Development Techdegree Graduate 21,199 Points

Donald,

My code will pass the challenge, but there are of course many ways to do this. Are you asking why your original code, and your new code do not pass?

well, I guess if you deleted the parentheses around dict when you return it, it will pass. Since you have created the dict outside the function, and your function is mutating the dict, then you may not need to return anything, but it depends on the instructions for the challenge and if they are testing by looking for a return of something.

Donald Tam
Donald Tam
1,570 Points

Dear Jame , thx for your reply. But I am looking the answer to pass the game.

would you or anyone can told me why I can not pass please?

I have learn some in your answer and modify as below:


dict={} key = None value = None

def word_count(string): keys = [bit.lower() for bit in string.split()] for word in keys:

    dict.update({word:keys.count(word)})


return (dict)

word_count("I do not like it Sam I Am")

Donald Tam
Donald Tam
1,570 Points

Dear James,

Yes, I am askinkg why my code do not pass

From your hints, I fix it finally.

Thx a lot