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 trialThomas Katalenas
11,033 Pointsword_dict
def word_count(arg) word_dict = {} word_dict[arg] = count if arg in dict: count += 1
my code is starting to confuse me now!!
2 Answers
eirikvaa
18,015 PointsI won't show you the solution, but I can point you in the right direction.
Firstly, remember that you are asked to count the occurrences of each word in the input string (which you have called arg). To do this you must isolate each word in the input string. You can do this with the split() function. Check example below:
my_string = "This is a string"
list_from_my_string = my_string.split()
# my_string.split() returns ['This', 'is', 'a', 'string']
From there you must go through the list of words that you got from using the split() function and count each time you see it. For each word you must ask yourself: If I haven't seen this word before, then I must insert the word and set its count to 1. If I have seen it before, I must add 1 to the count.
Lastly, remember that you can add a new key-value pair to a dictionary by using the word as the key and the count as the value. If the key doesn't exist in the dictionary, Python will create it.
my_dictionary[new_key] = value
I hope this helps. Don't hesitate to ask again if you're still stuck.
Thomas Katalenas
11,033 PointsHANKS
eirikvaa
18,015 PointsNo problem, glad I could help you out.
Thomas Katalenas
11,033 PointsThomas Katalenas
11,033 Pointsoooohhh kaay. The last part was the sum of my confusion. I was trying to use update() which takes two parameters, and then I couldn't figure out how to get the key and value into the update.