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

Curtis Scott
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Curtis Scott
iOS Development with Swift Techdegree Graduate 27,783 Points

i have no idea why i am not passing this challenge it works in workshop fine

it works fine in worship but does pass here i have no idea why

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):
        b = a.lower().split(" ")
        c = {}
        for word in b:
            if word in c.keys():
                c[word] = c[word]+1
            else:
                c[word] = 1


        return c

2 Answers

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Curtis,

You're making the (not unreasonable) assumption that you will only ever be passed strings that are separated by single spaces. The only clue you'll get from Treehouse that this is not the case is the error message says to split on "all whitespace", not just single spaces.

If there are other whitespace separators (e.g., multiple spaces or tabs) you will get the wrong output. You can test this yourself by passing your function strings like "hello world" and "hello\tworld" and seeing your output.

The remedy is to use the split() method without an argument. When passed no arguments, split uses its default which is to treat any amount of whitespace as a single space.

Hope that clears it up for you.

Cheers

Alex

Cameron Wood
Cameron Wood
4,840 Points

What does the console say?