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 trialDenis Frunz
15,929 PointsChallenge 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 ?
# 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
Python Development Techdegree Student 36,887 PointsThe 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.
Alex Koumparos
Python Development Techdegree Student 36,887 PointsHi 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
15,929 PointsHmmm.... okay, I pass single spaces to split() and the shown example has single space... what is the problem?
Denis Frunz
15,929 Pointsit seems I need expression operations to avoid different type of delimeters
Alex Koumparos
Python Development Techdegree Student 36,887 PointsNot 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
15,929 Pointshmmmm.... 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
Python Development Techdegree Student 36,887 PointsThat 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
15,929 PointsI 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
Python Development Techdegree Student 36,887 PointsI 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
15,929 PointsThanks a lot )
Denis Frunz
15,929 PointsIt worked when I lcosed and open a new page again
Denis Frunz
15,929 PointsDenis Frunz
15,929 Pointscould you help me to fix this?