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 trialIdris Abdulwahab
Courses Plus Student 2,961 PointsDictionaries
Code challenge: Make a function named word_count. It should accept a single argument which will be a string. The function needs to return a dictionary. The keys in the dictionary will be each of the words in the string, lowercased. The values will be how many times that particular word appears in the string.
My code worked when I tried it in Workspaces but it's not being accepted as a successful try in the code challenge. Please help look at it.
Thank you so much.
# 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):
vibes = string.lower()
new = vibes.split()
roses = {}
for word in new:
occurence = new.count(word)
roses.update({word:occurence})
return (roses)
2 Answers
Stuart McIntosh
Python Web Development Techdegree Graduate 22,874 PointsTry again as I got your code to work!
Abubakar Gataev
2,226 PointsI am not a prof but I think the argument has to be a string.