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

Dictionaries challenge. Please help

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.

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.

Here's my code so far:

word_count("this is my cool string"): my_dict = my_dict{word_count.split():len(word_count.split()}

I'm so lost on this and I've pretty much been lost on these last few challenges. Google says I'm at the stage where people usually give up, but I'm not going to. So you may see some pretty basic questions where I'm really confused.

Thanks!

When you add to a dictionary you add like this: (key1 ,key2 ,value1 ,value2 are just some random variables.)

my_dict = {} # here I created a new empty dict
my_dict[key1] = value1   # here I added a new key and gave it a value
my_dict[key2] = value2   # here I added another key and gave it a value

{key1:value1, key2:value2}  # This is what the dict would look like if you were to print it

I know that's not the complete answer but it should put you on the correct path.

1 Answer

Hello my friend, here's a way you can do it:

string = "I am that I am"

def word_count(string):
  lower = string.lower()
  words = lower.split()
  my_dict = {}
  for chars in words:
      c = 0
      for i in range(len(words)):
        if chars == words[i]:
          c += 1
          my_dict.update({chars:c})
  return my_dict

Hope this helps :) have an awesome night!

That actually didn't work :(

Why is that? Of course it works :)

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Your code never sets a value for words that aren't already in the dict.

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

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

This is how I usually recommend solving it. There are a few different ways to handle the middle bit, though.

Hi Kenneth Love!

Are you referring to my code?

If that so, can you give me an example please? Because it works for me :)

Thanks