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

What am I doing wrong? This runs flawlessly on PyCharm...

The output (tried with several strings) is as desired!

{'i': 2, 'do': 1, 'not': 1, 'like': 1, 'it': 1, 'sam': 1, 'am': 1}
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(str_input):
    lowercase_list = str_input.lower().split(" ")
    dict_output = {}

    for item in lowercase_list:
        item_count = lowercase_list.count(item)
        dict_output.update({item: item_count})

    return dict_output

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! You're doing great! Your syntax and logic are spot on, but you happened to miss a tiny detail in both the instructions and in your test data sets. I'm guessing you didn't try any data sets that included a tab or a new line character. The instructions say to split on all whitespace, but currently, you are only splitting on spaces.

This, however, is easily fixed. Using the split method without any arguments causes the string to be split on all white space including tabs and new lines.

Take a look:

lowercase_list = str_input.lower().split()

If I simply remove your " " from the arguments, your code passes with flying colors! Good job! :sparkles:

You're absolutely right! I didn't know about the split method without arguments. Thanks!