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

word count

I've put this in workspaces a few times and got the results I wanted but it's still saying it's not passing. Any ideas?

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(value):
    dictionary = {}
    new_list = []
    value = value.lower()
    my_list = value.split(" ")
    print(my_list)
    for word in my_list:

        if word == word in new_list:
            number += 1
            dictionary[word] = number

        else:
            number = 1
            new_list.append(word)
            dictionary[word] = number
            continue

    print(dictionary)
    return dictionary

word_count("I I I I do Do do Not not nOt")
word_count("I do not like it Sam I am")

2 Answers

This challenge really is a pain in the but, the feedback isn't at all useful.

value.split(" ")
    should be
value.split()

using split(" ") will only split on a single whitespace using split() will split on all whitespace

Also in your if block you are setting number += 1 this is casing all kinds of weirdness as the value is not reset in each loop, a better way to handle this would be just to set dictionary[word] += 1

Here is how I would do this.

def word_count(value):
    dictionary = {}
    for word in value.lower().split():
        if word in dictionary.keys():
            dictionary[word] += 1
        else:
            dictionary[word] = 1
    return dictionary

Cheers!

Awesome dude thank you.

Paul Heneghan
Paul Heneghan
14,380 Points

I'm wondering if it's because you called the function twice?