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 trialSean Flanagan
33,235 PointsCan't figure this out
Hi.
I can only create the function along with its required arguments. I can't figure out the rest.
# 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):
word_list = 0
3 Answers
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 PointsWe know that it wants us to return a dictionary of word counts. So start off by creating a variable with an empty dictionary.
We also know that it's going to give us a string that is a sentence with some words in it, so we can use the split method on strings to give us back a list of words.
We know that we need to convert the string and words to lowercase
For each word in the string, we want to count how many times it's in the string, and put that count into the dictionary
Let me know if you have more questions.
psculthorpe
3,151 PointsI've got this, which works fine in my Workspace, but will not be accepted as the answer. Confused :(
def word_count(string):
words = string.lower().split(" ")
dictionary = {}
for word in words:
if word not in dictionary:
dictionary[word] = 1
else:
dictionary[word] += 1
return dictionary
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 Pointssplit(" ")
is only going to split on space characters. split()
with no arguments will default to splitting on any whitespace character, including tabs and new line characters.
psculthorpe
3,151 PointsOf course! Thanks Brendan Whiting :)
Sean Flanagan
33,235 PointsSean Flanagan
33,235 PointsThanks for your help Brendan Whiting.
I know what the split() and lower() methods do but I'm not sure what to use them on.
Here's what I've got so far:
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 PointsBrendan Whiting
Front End Web Development Techdegree Graduate 84,738 PointsYou can call those methods on the
string
parameter:string.lower().split()
. You'll end up with a list of lowercase words.Also, what we want to end up with is so that they dictionary has key value pairs where the key is a word and the value is the number of times that word occurs. The example they gave us is that
word_count("I do not like it Sam I Am")
would produce{'i': 2, 'do': 1, 'it': 1, 'sam': 1, 'like': 1, 'not': 1, 'am': 1}
. So as you loop through the words, you want to be making changes to the dictionary object, rather than theword_list
.One way to do this is check to see if that word already is a key in the dictionary. If it is, increment the value by one. If it's not, add that key to the dictionary with the value 1.
Sean Flanagan
33,235 PointsSean Flanagan
33,235 PointsHi Brendan.
I've cracked this with your help and the help of psculthorpe.
Thank you both!