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

Douglas Brunson
PLUS
Douglas Brunson
Courses Plus Student 2,458 Points

Having problems with the "word count" code challenge.

This is my code. I've tried it out with different strings while adding various extra spaces between letters and I'm getting the right output when printing my_dict at the end. For some reason, I keep getting the error message "Bummer. Hmmm, didn't get the right output."

Can anyone help me with this? I've spent several hours on this activity.

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(my_string):

#This block is for producing a list called my_list of the words in the inputted string,
#but with the spaces removed.
# E.g. This section can be used with the example string to produce:
# my_list = ['i','do','not','like','it','sam','i','am']
    little_string = my_string.lower()
    little_string += ' '
    new_string = ''
    my_list = []
    for item in little_string:
        if item != ' ':
            new_string += item
        else:
            my_list.append(new_string)
            new_string = ''
#This block for creating the dictionary.
    my_list_copy = my_list

    val = 0
    values = []

    for item in my_list:
        for otheritem in my_list_copy:
            if item == otheritem:
                val += 1
            else:
                True
        values.append(val)
        val = 0

    my_dict = {}
    for index in range(len(my_list)):
        key = my_list[index]
        val = values[index]
        my_dict[key] = val
#This final if statement for removing any empty strings from my_dict.
    if '' in my_dict.keys():
        del my_dict['']

    return my_dict

2 Answers

Venkat SOMALARAJU
Venkat SOMALARAJU
2,448 Points
<p>
# 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_input):
    string_input = string_input.lower()
    #out_list = []
    string_input = string_input.replace(",","")
    our_list = string_input.split(" ")
    dict = {}
    for word in our_list:
        try:
            count = int(dict[word])
            if count>0:
                dict[word] = count + 1
        except:
            dict[word] = 1
    return dict
</p>
Douglas Brunson
PLUS
Douglas Brunson
Courses Plus Student 2,458 Points

What does the <p> and </p> mean? I tried your code between these two and it did not work for me.