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

Nancy Melucci
PLUS
Nancy Melucci
Courses Plus Student 36,143 Points

"Bummer! Try again!" doesn't help me figure out how to fix this one.

This works in my IDE and gives a correct output but doesn't work here in code challenges. The feedback is minimal. Anyone out there who can clue me in as to what I am missing?

wordcount.py
# E.g. word_count("I do not like it Sam I Am") gets back a dictionary like:
# {'i': 2, 'do': 2, 'it': 1, 'sam': 1, 'like': 1, 'not': 1, 'am': 1}
# Lowercase the string to make it easier.

def word_count(mystring):
    string_dict = {}
    mystring = "Nobody expects the Spanish Inquisition"
    mystring = mystring.lower()
    for word in mytring.split():
        if word in string_dict:
        string_dict[word] += 1
    else:
        string_dict[word] = 1
    return string_dict

1 Answer

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

Hi there! You're sooooo close here! So I'm going to give some hints.

  • Erase this line: mystring = "Nobody expects the Spanish Inquisition" this is overwriting what Treehouse is sending in. Treehouse will give the string to be evaluated and it comes into the function through the parameter.
  • You have a typo. You've typed: for word in mytring.split(): when you meant to type for word in mystring.split():.
  • Your indentation is incorrect. Take a closer look at the indentation in your if/else clauses

So if I comment out that line, fix a tiny spelling error, and correct the indentation, your code passes! Your logic is spot on, it's just some tiny details that are getting in the way.

Good luck! :sparkles: