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

Nikhil Alexander
Nikhil Alexander
1,444 Points

i dont know how to get the value of the number of times a word appears in a string...

please help out

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(words):
    dictionary = {}
    sentence = []
    for word in words:
        sentence.append(word)

        dictionary.update({word:

2 Answers

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Nikhil,

First, think carefully about what Treehouse is giving you as an input to your function. You are calling it 'words' but you are being told that the thing you are getting is:

It should accept a single argument which will be a string.

So this is really one long 'word' that you need to break into the constituent words. The typical string method you would use for this is 'split'.

It looks like you might think that your for word in words is doing the splitting for you, after all, the code is valid syntactically so it must be iterating over something, right? True, it is iterating over something but in this case it is individual characters (remember that strings behave like lists of characters) not words.

Next you are manually incrementing an index to get through your iterable. But remember that you don't manually iterate for in (you have to set the iterator in old C-style for loops, and while loops) but in a for in loop, Python will automatically work through each element in the iterable.

Next you are comparing sen to itself, which will always be True (if sen == sen). I'm not sure exactly what you are trying to do with this conditional. I think maybe you are trying to check if sen is in the dictionary. With your variable names, you could do this by writing:

if sen in dictionary:

Which will check if sen is a key in dictionary. Having established that sen is a key in dictionary we can use the basic syntax to update the value:

dictionary[key] = dictionary[key] + 1  # we can make this shorter by using the `+=` operator

Remember that you also have to handle the case where sen isn't in dictionary (i.e., the first time sen appears in your loop). In that case you can update the dictionary using the same syntax but instead of giving it the the oldValue plus a difference, you just give it the initial value of 1.

Be careful with what types you are giving your dictionary. In your last line you are turning your number into a string, which you don't want to do.

Lastly, remember that your function needs to return something.

Cheers

Alex

Nikhil Alexander
Nikhil Alexander
1,444 Points

thank you so much for your help Alex.... this was my passing code

def word_count(words):
    dictionary = {}
    lowered_words = words.lower()
    sentence = lowered_words.split( )

    for sen in sentence:
        if sen in dictionary:
            dictionary[sen] += 1
        else:
            dictionary[sen] = 1

    return dictionary

thank you for your time and sticking with this all these days.. ~Nikx222

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Nikhil,

Instead of thinking in terms of counting the number of times a substring appears in a string, think about whether you can do each of the following:

  1. split a string into words;
  2. check if a key exists in a dictionary
  3. add 1 to a value to a in a dictionary If you can do the above, you can achieve the effect of counting the number of times a substring appears in a string.

Hope that gives you the necessary pointers

Cheers

Alex

Nikhil Alexander
Nikhil Alexander
1,444 Points

I took your advice Alex .... and this is what i came up with

def word_count(words):
    dictionary = {}
    sentence = []
    for word in words:
        sentence.append(word)

    index = 1
    for sen in sentence:
        if sen == sen:
        index += 1


    dictionary.update({"{}".format(sen):"{}".format(index)})

can you point out where am i making a mistake?? thank you for helping me out all this time Id appreciate any help... ~ Nikx222