Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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?

# 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

28,521 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.