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 trialAryan Monga
4,061 PointsA quiz question!
Can i get to know what is wrong with my code? Thanks!
# 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(a):
dictonary={}
count= 1
a=a.lower()
lista=a.split(" ")
for word in lista:
if word in dictonary:
dictonary[word]= count + 1
else:
dictonary.append(word)
dictionary[word]=1
print dictonary
1 Answer
Steven Parker
231,248 PointsI spot a few issues:
- to split on any whitespace, leave the argument to split empty
- you spelled "dictionary" two different ways (also "dictonary") — either will work but it must be consistent
- the "count" variable is not needed or useful, just increment the directory entry itself
- you can increment this way "
dictonary[word] += 1
" - dictionaries don't have (or need) an append method
- the assignment of the value to the dictionary key will create a new entry by itself
- you need to return the result, you won't need to print anything