Python Python Collections Dictionaries Word Count

Craig Brooks
Craig Brooks
3,255 Points

Output looks very similar but I get 'Bummer'

I wrote my code in PyCharm (it is pasted at the bottom in comments). But when I bring it into here I can seem to pass it. I even tried using your example at the top for confirmation and it outputs the same (minus order, which shouldn't matter on a dict right?).

Can someone please check my code for what I'm doing wrong? I feel my answer is right but it's kind of hard to continue with a false sense of confidence haha.

Thanks!

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):
    make_dict = []
    make_list = list(string.lower().split(" "))
    for word in make_list:
        make_dict.append([word, make_list.count(word)])
    return dict(make_dict)

# Below is my code from PyCharm

# tester = "I do not like it Sam I Am"

# def word_count(string):
#     make_dict = []
#     make_list = list(string.lower().split(" "))
#     for word in make_list:
#         make_dict.append([word, make_list.count(word)])
#     print(dict(make_dict))
# 
# word_count(tester)

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

1 Answer

Steven Parker
Steven Parker
168,640 Points

The "Bummer" message gave you a hint: "Be sure you're lowercasing the string and splitting on all whitespace!"

To split on "all whitespace", the argument to "split" should be left empty or set to None. For more details, see the documentation page on "split".

Craig Brooks
Craig Brooks
3,255 Points

Holy moly! The most fun part of learning to code is realizing those small nuances that fix it when you sit and try over and over and over...or when Steven comes to the rescue haha. Thank you!

Steven Parker
Steven Parker
168,640 Points

It warms my heart to think I might be a "most fun part of learning to code". :blush: