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

Matte Matt
Matte Matt
2,354 Points

I don't understand whats wrong

Ive tried it with multiple phrases and it seems to work but the thing doesn't accept it, all it says is "Bummer Try Again"

Anyone know why?

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.

#Function That Returns a dictionary with each word in the string and its frequency.
def word_count(string):

    #Variable Assignments.
    string = string.lower()
    words = string.split()
    wordCount = {}
    freq = 0

    #Assigning the key:value pairs.
    for i in range(0, len(words)):
        currentWord = words[i]
        freq = 0

        #Finding the frequency of each word
        for j in range(0, len(words)):            
            if words[j] == currentWord:
                freq += 1
        wordCount[words[i]] = freq

    return wordCount

word_count(input("Enter your string: "))

1 Answer

andren
andren
28,558 Points

The issue is that you are doing more than the challenge asks for. The challenge asks you to create a word_count function, and that is all you are supposed to do. You are not supposed to call it, and you are not supposed to prompt for input with the input function.

The challenge checker itself will call your function and pass in a string, it is not setup to provide input through the console like the input function is expecting. And that is true for all challenges with the exception of challenges that explicitly asks you to prompt users for input.

So since your word_count function works, all you need to do is remove your last line of code. Then your code will pass the challenge.