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 trialRyan Lail
4,386 PointsCreate a function named word_count()...
Create a function named word_count() that takes a string. Return a dictionary with each word in the string as the key and the number of times it appears as the value.
This is my code, yet it says that some of the words are missing. Why is this?
# E.g. word_count("I am that I am") gets back a dictionary like:
# {'i': 2, 'am': 2, 'that': 1}
# Lowercase the string to make it easier.
# Using .split() on the sentence will give you a list of words.
# In a for loop of that list, you'll have a word that you can
# check for inclusion in the dict (with "if word in dict"-style syntax).
# Or add it to the dict with something like word_dict[word] = 1.
def word_count(string):
string.lower()
string.split()
dict = {}
for word in string:
for key in dict:
if word == key:
dict[word] = 1
else:
dict.update({word: 1})
return dict
2 Answers
Martin Cornejo Saavedra
18,132 PointsThere are some errors, you have to redefine string when you apply lower() and split(), else the change won't be saved. And I modified the rest of the code, you cannot iterate over the keys of a dict if you are creating that dict, there will be no key to iterate over.
def word_count(string):
string_v2 = string.lower()
string_v3 = string.split()
dict = {}
for word in string_v3:
if word not in dict:
dict[word] = 1
else:
dict[word] += 1
return dict
Graham Mackenzie
2,747 PointsHey Ryan!
So, there are a few things going on here which are contributing to your troubles:
- The first thing is that you need to assign
string.lower()
to a new variable. Otherwise, it's like you didn't call.lower()
at all. - The next thing is that you need to apply
.split()
to the variable that you assignedstring.lower()
to, and assign that to another new variable. Again, if you don't assign the results of these calls to new variables, it's like they never happened. - The next issue is that you want to get in the habit of avoiding using built-in types as variable names.
dict
is a built-in Python type (as isstring
, in the formstr
), so - even though it will work as is - it would be best to give it a more unique name, such asmy_dict
, or some such thing. - Next, is the logic of your
for
loop. You've got the first part right, but there is no need to nest the secondfor
loop within the first one; you can just check eachword
in your list of lowered words split from the passed-in string. If the word is in your dictionary, increase the value of that dictionary key by 1, otherwise, set the value to 1.
I hope this helps! Please mark this as the Best Answer if you found it most helpful.
Thanks! and Be Well, Graham
Parvinder Grewal
3,678 PointsHey Graham,
I do not understand how the the expression dict[word] = 1 . Does this append the word to a the first position to in the dictionary? I thought we cannot set the order.