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

Not sure what I am missing here - maybe incorrect in updating dict? help please :)

unfortunately the only feedback I am getting for why the code isn't coming up with the correct result is "bummer: try again!" any insights would be helpful, thank you

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 = (input())
    split_string = input.lower().split()
    dict = {}
    for key in split_string:
        dict.update({"key": split_string.count(key)})
    return dict

1 Answer

ร˜yvind Andreassen
ร˜yvind Andreassen
16,839 Points

You have two problems with your code.

When you're defining your word_count function, you are using syntax witch indicates to Python that this should be a variable, and not a function. varibale_name = variable_value. You are also trying to feed your function an function as an input without any actual input.

This challenge could be solved like this.

def word_count(input):
    split_string = input.lower().split()
    dict = {}
    for key in split_string:
        if dict.get(key):
            dict[key] = dict[key] + 1
        else:
            dict[key] = 1
    return dict

Checking if a word has been assign as a key in dict and either updating the value or setting the key and initial value.