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 (Retired) Dictionaries Word Count

Siva Krishna Raju Vegesina
Siva Krishna Raju Vegesina
1,877 Points

Help in resolving a Challenge Task

Hi,

Could you please let me know what is wrong and what Am I missing as the out put give me correct when checked in python Idle but here in the site displays "Bummer! Didn't get the right count for some of the words."

Create a function named word_count() that takes a string. Return a dictionary with each word in the string as the key and the number of times it appears as the value.

Here is my program:

def word_count(str1): str1_list = str1.lower().split() str1_dict = {} for word in str1_list: count = 1 if word in str1_dict: count += 1 str1_dict[word] = count else: count = 1 str1_dict[word] = count return str1_dict

Below is the sample output I got when checked in python idle

str1 = 'siva krishna raju vegesina raju rama reddy vegesina krishna siva yadav mujeeb john sameer'

word_count(str1) {'vegesina': 2, 'mujeeb': 1, 'reddy': 1, 'siva': 2, 'john': 1, 'rama': 1, 'yadav': 1, 'krishna': 2, 'sameer': 1, 'raju': 2}

word_count.py
# E.g. word_count("I am that I am") gets back a dictionary like:
# {'i': 2, 'am': 2, 'that': 1}
# Lowercase the string to make it easier.
# Using .split() on the sentence will give you a list of words.
# In a for loop of that list, you'll have a word that you can
# check for inclusion in the dict (with "if word in dict"-style syntax).
# Or add it to the dict with something like word_dict[word] = 1.

def word_count(str1):
    str1_list = str1.lower().split()
    str1_dict = {}
    for word in str1_list:
        count = 1
        if word in str1_dict:
            count += 1
            str1_dict[word] = count
        else:
            count = 1
            str1_dict[word] = count
    return str1_dict

2 Answers

You actually don't need the internal count variable in the loop, just increment or set the value for the matching key in the dict directly.

Chris Freeman
Chris Freeman
Treehouse Moderator 67,986 Points

For example:

str1_dict[word] = 1

or

str1_dict[word] += 1

Matt Nickele
Matt Nickele
468 Points

For the if statement you are adding 2 values at a time. The for loop starts with count equal to 1 and then the if statement adds 1 again. If a word only appears once you are fine, but if it is more than one time you are adding two for every appearance over 1. I dont think you can use one variable for count either. You need count to hold its value for words repeating often but your code will reset count each time. Try printing your values, I have a feeling as is repeating words all have a value of 2

Siva Krishna Raju Vegesina
Siva Krishna Raju Vegesina
1,877 Points

Thanks Matt Nickele for explaining what is wrong in detail.

Appreciate your help.