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 trial

Python Python Collections (2016, retired 2019) Dictionaries Word Count

Am I an idiot? I am doomed?

These challenges are sooo confusing and strange. I think this is the 5th time i'm asking for help. I think I know nothing ;( Okay! This is the question "Alright, this one might be a bit challenging but you've been doing great so far, so I'm sure you can manage it. I need you to 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. Check the comments below for an example." Please help me! I am getting overwhelmed and confused.

wordcount.py
# 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):
    a=a.lower()

1 Answer

Stephen Cole
PLUS
Stephen Cole
Courses Plus Student 15,809 Points

Break your tasks into smaller parts and ask yourself how to do each one.

# Create an empty space to put my words and the number of times they occur. 
# Maybe a Set or a Dictionary will work.

# Next, take my string and *split* them into a list.

# Iterate through my list one item at a time.
# Check the word to see if it is in my Dictionary.
# If a word appears for the first time, add it to my Dictionary and give it a value of 1.
# Otherwise, add 1 to the total.

# Print the Dictionary and see if the totals match.

Sketch out what you want to do before you start. Then, like a game of Mad Libs, all you have to do is fill in the blanks.