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 trialJirka Michalik
11,573 PointsAlright, this one might be a bit challenging but you've been doing great so far, so I'm sure you can manage it.
What the heck? This code is again working only in my python IDLE, but Treehouse didn't accept this solution :/
Can you help me please?
# 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):
dictionary = {}
list_with_string = string.lower().split(" ") # small chars in list
for item in list_with_string:
value = list_with_string.count(item)
dictionary[item] = value
2 Answers
Krishna Pratap Chouhan
15,203 PointsThe problem with your code is.... "Incorrect Indentation" Try this... i corrected the indentation...
Plus i also changed "split" function
def word_count(string):
dictionary = {}
list_with_string = string.lower().split() # small chars in list
for item in list_with_string:
value = list_with_string.count(item)
dictionary[item] = value
return dictionary
Hope this helps! if correct then please do accept the answer. Have fun coding.
Jirka Michalik
11,573 PointsOh okay - it's the right solution ... thank you
Swapnil Paralkar
3,359 PointsSwapnil Paralkar
3,359 PointsJus a quick question of this . Read somewhere that python Dictionary do not support duplicate keys . If we want to show duplicate keys . What would be the workaround?
Krishna Pratap Chouhan
15,203 PointsKrishna Pratap Chouhan
15,203 PointsSwapnil Paralkar , you cannot write duplicate keys in dictionary.
What you can do though, is make dictionary a list of values and then append values to those lists.
If you want this as a functionality, you might want to see into... defaultdict
https://docs.python.org/3/library/collections.html#collections.defaultdict