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

Zach Anderson
Zach Anderson
3,691 Points

Two solutions in Workspaces, neither accepted by this Challenge

Hello!

If you look at the code attached to this post you'll see I have two commented lines. If I uncomment either of them then I get (what I assume is) the correct answer... but only in Workspaces testing.

The output I get (when using the sample string) is:

{'do': 1, 'it': 1, 'like': 1, 'not': 1, 'sam': 1, 'am': 1, 'i': 2}

I confirmed that this is a dict by doing:

print(type(word_count("I do not like it Sam I Am")))

...which returned <class 'dict'>

Every Key is a string word from the original sentence, lowercased. Every Value is an integer representing the number of times a word appeared in the original sentence.

I get similarly (seemingly) correct results for different strings as well.

But this Challenge/quiz doesn't like either of those two solutions/commented lines.

What am I missing here?

Thanks!

wordcount.py
def word_count(stir):
    stir = stir.lower()
    werds = stir.split(' ')
    dikt = {}
    for werd in werds:
        # dikt.update({werd:werds.count(werd)})
        # dikt[werd] = werds.count(werd)
    return dikt
Dave Laffan
Dave Laffan
4,604 Points

You seem to be using 'words' and 'werds' instead of one or the other.

Zach Anderson
Zach Anderson
3,691 Points

Thanks for the catch, Dave. Unfortunately that didn't resolve the problem I was having. I changed the spelling of the variables after I ran into issues to see if I had been unintentionally using a keyword or reserved word... but no such luck.

Any other suggestions? (I updated the spelling in the main post to fix the inconsistency)

3 Answers

I think you guys are looking forward to the difference between .split(' ') and .split(). Let me list the difference below:

split() splits on all whitespace including spaces, tabs, newlines, vertical spaces, etc. .split(' ') splits only on spaces.

I hope this helps :grin:

~Alex :sparkles:

Zach Anderson
Zach Anderson
3,691 Points

Interesting... I suppose it's possible other whitespace characters are being used by the test/evaluation program. In fact, it's likely that it's MY assumption that the function will be presented with a clean, single-line sentence whereas the question asks to separate the words regardless of what surrounds them.

Thanks for catching that fine point Alex!

Cheers, -Zach

Dave Laffan
Dave Laffan
4,604 Points

Ah ok, in that case. change

werds = stir.split(' ')

to

werds = stir.split()

Why this is, I have no idea, but it is :smile:

Zach Anderson
Zach Anderson
3,691 Points

Dave, that worked! Thanks!! I do not know either... I've had problems in the past with macOS trying to do directional/tilted quotation marks which is actually a different character... but that doesn't seem to be the issue here.

If anyone else could chime in as to why this would fix it I'd love to know!