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

Alistair Jenkins
Alistair Jenkins
16,761 Points

Hello. Can anyone explain? My code works fine in Pycharm not in challenge editor (??) Thank you! Alistair

My code seems to follow logically and, as I say, gives the correct result in a test script I have written in Pycharm (code is copy and pasted from there, test script includes a print statement to show results). My code does not work in the challenge editor though ??

All help would be appreciated, Thank you, Alistair

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):
    count = {}

    for word in words.lower().split(""):
        if word not in count.keys():
            count[word] = 1
        else:
            count[word] += 1
    return count

3 Answers

Manish Giri
Manish Giri
16,266 Points

You just need to fix this line - for word in words.lower().split(""):.

This splits on every character in the string, you should be splitting on whitespace only. So, either use the default .split() (which splits on whitespace), or use a space to split on - .split(" ").

Rest of the code looks good.

Ryan Cross
Ryan Cross
5,742 Points

i ran your code on a sample on my ide...i got back ValueError: empty separator i think you maybe didnt put a seperator in your split...needs something to split on...whitespace, a colon...something .split("") ...split where? also watch for the whitespace thing with split(" ") v split() from the docs If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a None separator returns [].

Alistair Jenkins
Alistair Jenkins
16,761 Points

Thank you chaps. Working fine now!