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

MONTRIAL HARRELL
seal-mask
.a{fill-rule:evenodd;}techdegree
MONTRIAL HARRELL
Python Web Development Techdegree Student 2,150 Points

Challenge says my results do not match but I can not see what is wrong.

My code for word_count challenge appears to be correct. I have double checked my out put and everything appears to be right but I keep getting Bummer message. Below is my code. Can someone tell me what is wrong?

def word_count(string_1):

key_list = []
value_list = []
return_dict = {}
string_1 = string_1.lower()
string_list = (string_1.split(" "))
len_list = len(string_list)
counter = int(1)

while counter < len_list:
    a = string_list.count(string_list[counter])
    b = string_list[counter]
    key_list.insert(0,b)
    value_list.insert(0,a)
    #print("key = {} and value = {}".format(b,a))
    counter += 1
return_dict = dict(zip(key_list,value_list))
return return_dict
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_1):

    key_list = []
    value_list = []
    return_dict = {}
    string_1 = string_1.lower()
    string_list = (string_1.split(" "))
    len_list = len(string_list)
    counter = int(1)

    while counter < len_list:
        a = string_list.count(string_list[counter])
        b = string_list[counter]
        key_list.append(b)
        value_list.append(a)
        #print("key = {} and value = {}".format(b,a))
        counter += 1

    return_dict = dict(zip(key_list,value_list))

    return return_dict 

2 Answers

Steven Parker
Steven Parker
231,275 Points

Your solution is a bit unconventional but essentially sound. The problem was hinted at in the error message: "Bummer: Hmm, didn't get the expected output. 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. Providing a space causes it to split only on individual space characters.

Fix that and you'll pass the challenge.

unknown member
unknown member
Courses Plus Student 18,949 Points

Drop the shortest possible solution, thanks in advance.

MONTRIAL HARRELL
seal-mask
.a{fill-rule:evenodd;}techdegree
MONTRIAL HARRELL
Python Web Development Techdegree Student 2,150 Points

Steven - Thank you for your time and the answer. I am new to Python so my answer did take a unconventional route but will get better over time.

Thanks again!!