Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
216,135 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
216,135 Points

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