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

Dictionary challenge word count python

I thought my logic made sense and maybe it was the syntax but after trying several ways it just didn't work. I've seen several variations of how other people did it but others used .count() and couter(). I think this is something that can be solved using basic syntax taught in the video. Basicly looping and replacing new value if the same keyword was found. Plase let me know what I did wrong.
Thank You

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.

paragraph = "ceñir propinar elucubrar dilapidar ceñir propinar"
    #problem: when feed with a long string, it needs to be split into words, with spaces

def word_count(x):
    x = x.lower()
    split_x = x.split(" ")
    empty_dict = {}

    for word in split_x:
        if not in empty_dict:
            #add key and count #
            empty_dict["word"] = 1

        else:
            empty_dict["word"] = empty_dict"["word"] + 1
    return empty_dict

word_count(paragraph)

2 Answers

Wade Williams
Wade Williams
24,476 Points

You're really close! Your logic is sound and method is simple and straight forward, you just have some syntax errors.

def word_count(x):
    x = x.lower()

    # Split on ALL white space not just spaces by not passing anything into split()
    split_x = x.split()
    empty_dict = {}

    for word in split_x:

        # Forgot to add word into condition
        if word not in empty_dict:

            # Should be empty_dict[word] not empty_dict["word"]
            # we want the variable word not the string "word"
            empty_dict[word] = 1

        else:
            empty_dict[word] = empty_dict[word] + 1

    return empty_dict

There's some other minor tweaks you could make to make it more readable. Here's my code:

def word_count(string):
    words = string.lower().split()
    word_counts = {}

    for word in words:
        if word in word_counts:
            word_counts[word] += 1
        else:
            word_counts[word] = 1

    return word_counts

Thanks for pointing out that I missed the word in the if statement. Also I was thinking of using lower() and split() in one line but didn't know if I could do it.

Wade Williams
Wade Williams
24,476 Points

My advice to you when you're unsure whether or not you can do something with code is to fire up your local editor or repl and try it. For me personally, learning from experimenting is far superior to watching videos or reading. Great job on this one.