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

word_count code challenge in the Python Collections lesson on Dictionaries: code works in workspace, not in challenge

Hello,

I've seen a couple of weird things when working on this challenge.

Task: 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.

  1. the challenge asked me to write a function called word_count but then the checking seemed to indicate that it was looking for me to call the function I defined, not just write the function (kept getting an error "Where's word_count()" ...well such a call would not be in the function itself necessarily) , so I added the definitions for the my variables string and word_dict and added added the function call word_count(string), essentially making it a whole program. This made the "where's word_count()" error go away.

  2. the code checker asserts that my counts of the words are incorrect, but when I run a program with the same code in a Workspace, it works fine, I think. I built my function in a Workspace to begin with, tested it, and they placed the code into the challenge dialog. Message I get is "Bummer: some of the counts are incorrect" or something to that effect.

I'd add some screen shots but it looks like that's not an option.

Here's the code I had in my workspace, which aside from a print (so I could see the dictionary contents and test it) is the same as what's in the challenge dialog.

string = 'I am what I am' word_dict = {}

def word_count(string): lower_string = string.lower() word_list = lower_string.split() for word in word_list: if word in word_dict: count = word_dict[word] count += count word_dict[word] = count else: word_dict[word] = 1 print(word_dict) return word_dict

word_count(string)

Any help appreciated.

Thanks,

JH

Check out the Markdown Cheatsheet to see how to include code in your questions!

def word_count(item):
    dict1 = {}
    item2 = item.split() # splits by whitespace as standard (produces a list)
    for word in item2: # so for each string in the list,
        if word in dict1: # if that string is already in dict1,
            dict1[word] += 1 # increment the value of the key ('word') by 1.
        else:                # (as dict['key'] gives the value for that key)
            dict1[word] = 1  # if the word is not already in dict1, the value for that word is set to 1 (the key 'word' is set also!)
    return dict1

# This would output: {'word1': count, 'word2': count, 'word3': count}

3 Answers

def word_count(item):
    dict1 = {}
    item2 = item.split() # splits by whitespace as standard (produces a list)
    for word in item2: # so for each string in the list,
        if word in dict1: # if that string is already in dict1,
            dict1[word] += 1 # increment the value of the key ('word') by 1.
        else:                # (as dict['key'] gives the value for that key)
            dict1[word] = 1  # if the word is not already in dict1, the value for that word is set to 1 (the key 'word' is set also!)
    return dict1

# This would output: {'word1': count, 'word2': count, 'word3': count}

hope this helps

Robert Peters: thanks for your help - got it working.. My script gave me the same results as yours if I ran it in the workspace but for some reason if I did a slight rewrite to match yours more closely, the new function passed the code challenge. What is weird is that I kept getting an error with mine that said "where's the word_count()?" like I was supposed to call the function, whereas yours did not receive that error, and it doesn't contain any call to the the word_count function. So that was a confusing error message.

Peter Lawless: I didn't see any link to a markdown cheatsheet when I was creating the post, but I see it here in the Answers section. Thanks for pointing that out. Wish I could have added screen shots of some of the error messages too.