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

Luca Tardito
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Luca Tardito
Front End Web Development Techdegree Graduate 17,602 Points

hy guys, I've tried a lot of time to understand where is the problem in my code but I didn't find it....

when I tried to check the work the system said "different output"

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(string):
    my_list = string.lower().split(" ")
    dictionary = {}
    for word in my_list:
        if word in dictionary:
            dictionary[word] += 1
        else:
            dictionary.update({word:1})
    return dictionary

2 Answers

andren
andren
28,558 Points

The issue is that the challenge wants to you to split on all whitespace. While a space is an example of whitespace it is not the only example: tabs, line breaks, multiple spaces and other things like that are also classified as whitespace.

Conveniently enough the split method will actually split on all whitespace by default if you don't pass it any arguments, so if you remove your argument to it like this:

def word_count(string):
    my_list = string.lower().split()
    dictionary = {}
    for word in my_list:
        if word in dictionary:
            dictionary[word] += 1
        else:
            dictionary.update({word:1})
    return dictionary

Then your code will work.

Ignazio Calo
PLUS
Ignazio Calo
Courses Plus Student 1,819 Points

I tried to find the error but no luck.

def word_count(string):
    my_list = string.lower().split(" ")
    dictionary = {}
    for word in my_list:
        if word in dictionary:
            dictionary[word] += 1
        else:
            dictionary.update({word:1})
    return dictionary



response = word_count("I do not like it Sam I Am")
from_example = {'i': 2, 'do': 1, 'it': 1, 'sam': 1, 'like': 1, 'not': 1, 'am': 1}

print(from_example == response)

As you can see here your code generate an input equal to the example :(