Python Python Collections (2016, retired 2019) Dictionaries Word Count

victor escarcega
victor escarcega
7,599 Points

i ran it on IDLE and I get the correct output but the challenge is not accepting this.

what am I doing wrong?

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):
    dictionary = {}
    items = string.lower().split()
    for words in items:
        dictionary[words] = items.count(words)

2 Answers

Is return lined up with the for loop? The following passed for me.

def word_count(string):
    dictionary = {}
    items = string.lower().split()
    for words in items:
        dictionary[words] = items.count(words)
    return dictionary

You aren't returning anything. You need to return the dictionary.

victor escarcega
victor escarcega
7,599 Points

thank you that fixed part of the problem, now i get this

Bummer: Hmm, didn't get the expected output. Be sure you're lowercasing the string and splitting on all whitespace!