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

Why does this code not work in the Code Challenge but works in the terminal?

Hello, my name is D. and I am stuck on this particular code challenge. I wrote the exact same code in an offline IDE and ran it in my terminal using Python and it returned exactly what the course wanted; however, when I copy and past the same code and check my work , the code will not be accepted and I do not know where the error is. I would appreciate if anyone could help me understand where I went wrong. Thank you.

wordcount.py
def word_count(string):
    # Force string to lower case
    string = string.lower()
    # Split the string into a list
    word_list = string.split()
    # Create an empty dictionary
    dictionary = {}
    for word in word_list:
        dictionary[word] = word_list.count(word)
    return dictionary

4 Answers

Shabbir Nayeem
Shabbir Nayeem
2,642 Points

Hi D,

Great job on solving the problem. I ran your code, and it worked for me. Are you getting any error?

I've tried your code and it works. They may be some rogue whitespace hiding somewhere or the code challenge cached an incomplete version of your code. Refresh the entire page and paste it in there.

My code is similar and i'll leave it here just in case.

word_dict = {}

def word_count(sentence):
    word = sentence.lower()
    word_list = word.split()
    for word in word_list:
        word_dict[word] = word_list.count(word)
    return word_dict

Thanks for the response guys!

I'll try to close out the browser and reload to see if maybe that will solve the issue with the code not being accepted. And Sabbir, nope I'm not getting error on my local Python environment but on the code challenge I just get the 'bummer, try again response!'. No further guidance or any hint as to what the issue could be.

Alright that did it! I just shut down my browser and opened a new session and then the Code Challenge accepted my code. Thanks for the input, I really appreciate it!!