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 trialViktoras Domarkas
1,038 Pointswordcount. Seem to work, but cannot pas
This program seem to work on my python 3.6 editor, but on workspaces I get "Bummer!"
# 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):
string = (string.lower()).split()
dictionary = {}
i = 1
for word in string:
if word not in dictionary:
dictionary.update({word: i})
else:
dictionary.update({word: i+1})
return(dictionary)
4 Answers
james south
Front End Web Development Techdegree Graduate 33,271 Pointsyour code works as long as there are no more than 2 of a given word in the string. as a test case, try this: "I do not i i i not like sam sam like it Sam I Am". it will return that there are 2 i's when there are 5. this is because in your else block, the value of the word key is set to i + 1, and since i is not modified, i + 1 is always 2. you have a workable idea, just tweak it a little.
james south
Front End Web Development Techdegree Graduate 33,271 Pointsthat's a dictionary comprehension, not a list comprehension. there are also set comprehensions.
https://www.smallsurething.com/list-dict-and-set-comprehensions-by-example/
Viktoras Domarkas
1,038 PointsThank You so much james south and MR.7, not only helping with this particular exercise, but for all of that useful information that I'll look into
Donald Tam
1,570 Pointsdict={} key = None value = None
def word_count(string): list = string.split()
for i in list:
key = i.lower()
if key in dict:
value = dict[key]+1
dict.update({key:value})
else:
dict.update({key:1})
return(dict)
word_count("I do not i i i not like sam sam like it Sam I Am")
Hi Mr james south or any other
would you pls also help to advise where I go wrong??
MR. 7
24,079 PointsDonald Tam, have you solved this challenge?
Let us know if you still need help! :)
Never Stop Learning