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

On one of the final code challenges in python; my code creates the desired output but comes up wrong.

the code challenge is as follows: Alright, this one might be a bit challenging but you've been doing great so far, so I'm sure you can manage it.

I need you to make a function named word_count. It should accept a single argument which will be a string. The function needs to return a dictionary. The keys in the dictionary will be each of the words in the string, lowercased. The values will be how many times that particular word appears in the string.

Check the comments below for an example.

my code looks like this:

# 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.
def word_count(x):
    x = x.lower()
    lx =x.split(" ")
    sx = set(lx)
    dx = {}
    for s in sx:
      c= 0
      for l in lx:
        if d == l:
          c+=1
      dx[s] = c
    return dx

It generates the correct output when I test it in the console, so I'm not sure what i'm missing.

5 Answers

Nevermind. I figured it out.

def word_count(val): val=val.lower() dist={} list1=val.split(" ")

for x in list1:
    if x in dist:
        dist[x] +=1

    else:
        dist[x]=1

return dist

I am getting not sure of the issue

Kevin Lassar
Kevin Lassar
22,440 Points

Hey Treehouse,

Any idea why my code submission wouldn't be passing? I've tested all inputs both lower, uppercase, duplicates etc..

def word_count(string):
    split_words = []
    dictionary = dict()
    for word in string.split(" "):
        split_words.append(word.lower())
    for word in split_words:
        dictionary[word.lower()] = split_words.count(word)
    return dictionary

Code Challenege:

Alright, this one might be a bit challenging but you've been doing great so far, so I'm sure you can manage it.
I need you to make a function named word_count. It should accept a single argument which will be a string. 
The function needs to return a dictionary. The keys in the dictionary will be each of the words in the string, 
lowercased. The values will be how many times that particular word appears in the string.
Check the comments below for an example.

Here's what I got:

def word_count(string):
    text_list = string.lower().split()
    key_list = text_list
    value_list = []

    for words in key_list:
        value_list.append(text_list.count(words))

    zip_obj = zip(key_list, value_list)
    word_dict = dict(zip_obj)
    return(word_dict)