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

Craig Brooks
Craig Brooks
3,278 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
231,269 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,278 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
231,269 Points

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