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

Akshaan Mazumdar
Akshaan Mazumdar
3,787 Points

NOT WORKING

IT says didn't get expected output need to lowercase and split at whitespace.

I ran the same code in my editor it worked properly.

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):
    x=0
    dctn={}
    y=(string.lower()).split(" ")
    for letter in y:
        x=0
        dctn.update({letter:0})
        for checker in y:
            if letter == checker:
                x+=1
                dctn.update({letter:x})
    return dctn

Can I have the question please

yea we need to see the question first

Akshaan Mazumdar
Akshaan Mazumdar
3,787 Points

Receive a string . Return a dictionary with all the words in lower case along with the number of times each word occurs in the string(see comments in code)

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Christabelle Kamhere and Abigairl N Chigwededza, the challenge is linked at the top right of this question under the "View Challenge" button. Thanks for helping out in the Community! :sparkles:

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! I received your request for assistance, Akshaan Mazumdar and you're super close here. Part of the problem here is the data you're testing. The challenge asks you to split on all whitespace, but currently, you're only splitting on spaces. It should also split on whitespace such as tabs and newline characters and I'm guessing your test data didn't include any of those.

We split on spaces with this:

split(" ")

But we split on all whitespace with this:

split()

Simply removing the quotations and blank space from within the split function causes this to pass! Excellent work! :sparkles:

Akshaan Mazumdar
Akshaan Mazumdar
3,787 Points

Thank You !!! this is valuable information :)