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

divyam pandey
divyam pandey
2,388 Points

My code is working fine on my own system, but giving error on Treehouse quiz console

I need you to make a function named word_count. It should accept a single argument which will be a string. The function needs to return a dictionary. The keys in the dictionary will be each of the words in the string, lowercased. The values will be how many times that particular word appears in the string. getting error: Be sure to lowercase the string and split at all whitespaces. What could be wrong? Can anyone help please?

my code below:

def word_count(give_str): mydict={} str_=give_str.lower() strin=list(str_.split(' ')) for i in strin: if i!='': if i in mydict.keys(): mydict[i]+=1 else: mydict[i]=1

return mydict
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(give_str):
    mydict={}
    str_=give_str.lower()
    strin=list(str_.split(' '))
    for i in strin:
        if i!='':
            if i in mydict.keys():
                mydict[i]+=1
            else:
                mydict[i]=1

    return mydict

2 Answers

Oszkár Fehér
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Oszkár Fehér
Treehouse Project Reviewer

Hi, Divyam Pandey. I tried your code in my editor and yes it's working, though in the challenge it does not pass and the only reason it's the split() method, by default the split() method splits the string with split(' ') parameter, empty space so you don't need to give that. .split() without parameters, it splits on any whitespace, tabs, spaces, line feeds etc, and it also skips all empty strings as a result of this, you don't need this condition as well if i!='':. However, if an argument is given, it is treated as a single delimiter with no repeated runs. Now I don't truly understand why the quiz console doesn't accept this way when an editor like pycharm accepts. I hope this will help you out. Happy coding.

divyam pandey
divyam pandey
2,388 Points

Thank you so much @Oszkár Fehér, for explaining it so well and in so depth. I even don't know, I should have asked this or not. But after getting it right on my editor and wrong in the Treehouse quiz console so many time, I posted it. once again thanks for the quick help :)

FWIW I had the exact same issue. Thank you for the clarification.

A follow-up question: Is the method .split(" ") wrong? My point is that I have been trying to debug this minor snippet for an hour only to discover that, while potentially superfluous, it does not appear to be incorrect code but rather a case of "not how the editor wants you to do it." This isn't the first time I've seen this either. It is a tad frustrating since the code check does not differentiate between "the code does not appear to be returning the right result" and "while your answer was correct there are issues that are pythonic in nature."