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

Chris Sehnert
Chris Sehnert
30,857 Points

def word_count(words): works for me but is not accepted...

please show me a string that returns an improper result... ...so that I can figure out what needs to be fixed...

this code works on the example string....

I have a version that tests if letter.isalpha() that also works...but that would return funny results for strings with punctuation or apostrophes ...with the amount of specifics requested for this challenge ...I can't understand where I've gone wrong.....???

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(words):
    words = words.lower()
    words += ' '
    result = {}
    word = ''
    for letter in words:
        if letter is not ' ':
            word += letter
        elif word in result:
            result[word] += 1
            word = ''
        else:
            if word is not '':
                result[word] = 1
                word = ''


    return result
Zach Anderson
Zach Anderson
3,691 Points

I just ran into a quirk on this question as well, there's some interesting specifics to it that aren't entirely clear...

I'd try using the .split() function, it makes word extraction a lot easier! The only catch being (and this is where I ran into trouble) you have to use .split() and not .split(' ') because the test/evaluation script here actually will try to pass strings with other types of whitespace characters like newlines...

Please mark this as an answer if it helped!

Best of luck! -Zach