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

Viktoras Domarkas
Viktoras Domarkas
1,038 Points

wordcount. Seem to work, but cannot pas

This program seem to work on my python 3.6 editor, but on workspaces I get "Bummer!"

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()).split()
    dictionary = {}
    i = 1
    for word in string:
        if word not in dictionary:
            dictionary.update({word: i})
        else:
            dictionary.update({word: i+1})
    return(dictionary)

4 Answers

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

your code works as long as there are no more than 2 of a given word in the string. as a test case, try this: "I do not i i i not like sam sam like it Sam I Am". it will return that there are 2 i's when there are 5. this is because in your else block, the value of the word key is set to i + 1, and since i is not modified, i + 1 is always 2. you have a workable idea, just tweak it a little.

Viktoras Domarkas
Viktoras Domarkas
1,038 Points

Thank You so much james south and MR.7, not only helping with this particular exercise, but for all of that useful information that I'll look into

Donald Tam
Donald Tam
1,570 Points

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})



return(dict)    

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

Hi Mr james south or any other

would you pls also help to advise where I go wrong??

Donald Tam, have you solved this challenge?

Let us know if you still need help! :)

Never Stop Learning