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 trialAndy Christie
1,361 PointsPlease help me with this syntax code
I'm really having trouble with this please can you help me?
# E.g. word_count("I am that I am") gets back a dictionary like:
# {'i': 2, 'am': 2, 'that': 1}
# Lowercase the string to make it easier.
# Using .split() on the sentence will give you a list of words.
# In a for loop of that list, you'll have a word that you can
# check for inclusion in the dict (with "if word in dict"-style syntax).
# Or add it to the dict with something like word_dict[word] = 1.
def word_count("I am that I am"):
dict = {}
my_list = "I am that I am".split()
for item in my_list:
dict[item]
if item in my_list:
dict[item] += 1
else:
dict[item] = 1
return dict
1 Answer
Zubeyr Aciksari
21,074 PointsHi Andy, since it is telling us that word_count() takes a string as a value, it automatically assumes the value inside it a string so you don't have to put there a real string like: "def word_count("I am that I am"):"
& Since we working with the dictionary, you don't have to create a new list.
I hope this helps:
def word_count(string_a): dict_a = {} for word in string_a.split(): if word in dict_a: dict_a[word] += 1 else: dict_a[word] = 1 return dict_a
Andy Christie
1,361 PointsAndy Christie
1,361 PointsAhh thanks mate, I've sorted it now:)