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

Denis Frunz
Denis Frunz
15,929 Points

Challenge Task 1 of 1, Try again error. Word Count

I have tested my code in workspace and it works however when I try to pass the challange I run into the message "Bummer! Try again!".What am I missing ?

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(string):
    dict_word = dict()
    new_string = string.lower()
    list_word = new_string.split(' ')
    for item in list_word:
        dict_word.setdefault(item,list_word.count(item))
    return dict_word

7 Answers

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

The problem is that the shown example is just that, an example. It is not the whole test suite. The checker is passing one or more test strings and your function fails when passed the actual test strings.

Denis Frunz
Denis Frunz
15,929 Points

could you help me to fix this?

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

Hi Denis,

Your problem is with the argument you are passing the split() method. This will cause split to only break on single spaces. Although the example string they show you only has single spaces, Other test strings might have other forms of whitespace. When you call split() with no arguments it breaks on all whitespace.

Cheers

Alex

Denis Frunz
Denis Frunz
15,929 Points

Hmmm.... okay, I pass single spaces to split() and the shown example has single space... what is the problem?

Denis Frunz
Denis Frunz
15,929 Points

it seems I need expression operations to avoid different type of delimeters

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

Not sure what you mean exactly. But I copy-pasted your code into the challenge, made the change I originally suggested and the challenge passed.

Denis Frunz
Denis Frunz
15,929 Points

hmmmm.... let's try again I can get strings with different delimiters for example: ,-/. but I pass argument only whitepace,so if tester sends a string like this "I,do,not,like,it,Sam,I,Am" it causes a problem

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

That would be true if the challenge wanted you to break on other delimiters. But they don't, they only want you to break on whitespace, but they want you to break on all whitespace (not just single space characters). Don't take my word for it, here is the exact error message that your code generates:

Bummer: Hmm, didn't get the expected output. Be sure you're lowercasing the string and splitting on all whitespace!

Pay particular attention to where it says "splitting on all whitespace!".

This is why I suggested the change that I did, which causes split() to split on all whitespace. And this is why the challenge passes when that change is made.

Denis Frunz
Denis Frunz
15,929 Points

I tried I used split() but still run into the same error at the same time I check it in workspace all work fine there This is my code which I try to run

# 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(string):
    dict_word = dict()
    new_string = string.lower()
    list_word = new_string.split()
    for item in list_word:
        dict_word.setdefault(item,list_word.count(item))
    return dict_word
Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

I pasted that code into the challenge and it passed.

Try closing the page and reloading it. If that doesn't work, try a different browser (it worked for me on Safari Technology Preview Release 57, MacOS 10.13.3).

If it still doesn't work then you might need to escalate to Treehouse support, because the problem is no longer with your code.

Cheers

Alex

Denis Frunz
Denis Frunz
15,929 Points

It worked when I lcosed and open a new page again