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

wordcount: input a word and return a dictionary of each of the words in string lowercase (key) and count of word(value)

    def word_count(word):
    list1 = word.split(" ")
    dic = {}
    for i in range(0,len(list1)):
        count = 1
        for j in range(i+1,len(list1)):
            if(list1[j] == list1[i]):
                count+=1
        dic.update({list1[i].lower(): count})
    return(dic)

doubt: when i run the code in shell the output is : {'i': 1, 'do': 1, 'not': 1, 'like': 1, 'it': 1, 'sam': 1, 'am': 1}

why is my count variable not incrementing?

Mike Wagner
Mike Wagner
23,888 Points

It is difficult to figure out how your code is structured when you do not present it in the proper format. Please use the Markdown Cheatsheet and edit your question so that we can help you more capably.

your code acts like this:

word_count("hello francesco francesco francesco")

# i = 0;  j = 1;  hello != francesco;  count = 1
# i = 0;  j = 2;  hello != francesco;  count = 1
# i = 0;  j = 3;  hello != francesco;  count = 1

# dic = {"hello": 1}

# i = 1;  j = 2;  francesco == francesco;  count = 2
# i = 1;  j = 3;  francesco == francesco;  count = 3

# dic = {'hello': 1, 'francesco': 3}

# i = 2;  j = 3;  francesco == francesco;  count = 2

# dic = {'hello': 1, 'francesco': 2}

# i = 3;  j = 3;  francesco;  count = 1

# dic = {'hello': 1, 'francesco': 1}

so you need to change the function in a way that it doesn't update a word count once it has completed it.

You could probably do something like:

def word_count(word):
    list1 = word.split(" ")
    dic = {}

    for i in range(0, len(list1)):
        count = 1
        if list1[i] not in dic.keys(): # to avoid that the function repeats the count for a word already counted
            for j in range(i + 1, len(list1)):
                if (list1[j] == list1[i]):
                    count += 1
            dic.update({list1[i].lower(): count})
    return dic

Anyway your code could also be written like this:

def word_count(word):
    list1 = word.split(" ")
    dic = {}

    for i in list1:
        if i.lower() in dic.keys():
            dic[i] += 1
        else:
            dic.update({i.lower(): 1})

    return dic

3 Answers

Instead of being given the answer explicitly, it is better to work it out yourself but get hints from the community. If you follow my suggestions in my previous answer and step through the code in the debugger whilst monitoring after each step how your count variable changes, I am sure you will work it out.

That means : Use the debugger tool to step through each line of code individually. Do a print(count) to see how your count variable changes after each code line executes. Then you will understand when and why it goes to =1 , =2, etc.

I just did it and found the answer.

Thanks Jo! :)

I suggest you debug the code and check the count variable for each step inside your 2 nested loops. if you are not familiar with debugging, watch the python module https://teamtreehouse.com/library/write-better-python/buggy-logs/the-python-debugger

hint: think about when you need to update the dictionary.

your code didn't format properly because you used the single quote (') not the backtick (`). try tweaking your function a little, you don't need nested for loops for this one.

ok thanks. now i have used backstick. :) please clear my doubt now.(why my count is always equal to 1?)