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 trialNikhil Alexander
1,444 Pointsi dont know how to get the value of the number of times a word appears in a string...
please help out
# 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
Python Development Techdegree Student 36,887 PointsHi 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
Alex Koumparos
Python Development Techdegree Student 36,887 PointsHi 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:
- split a string into words;
- check if a key exists in a dictionary
- 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
1,444 PointsI 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
Nikhil Alexander
1,444 PointsNikhil Alexander
1,444 Pointsthank you so much for your help Alex.... this was my passing code
thank you for your time and sticking with this all these days.. ~Nikx222