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 Python Collections (2016, retired 2019) Dictionaries Word Count

RM Hrdr
RM Hrdr
3,491 Points

word count challenge

Hello, Team Treehouse and all the awesome people in the community;

I'm trying to attempt to solve this challenge by using tuples as the key of the dictionary and employ the .get method to return the key, value pair, however, the output is not appropriate as seen below:

Could anybody help me out on how to achieve the output? big thanks!

Blackbox:coding-challenges rmph$ python wordcount.py Enter any good word: I do not like it Sam I Am ('i', 'do', 'not', 'like', 'it', 'sam', 'i', 'am') 8

wordcount.py
# 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.
words = input("Enter any good word: ").lower().split()
# TODO: convert list to tuples so it can be used as a key
t_words = tuple(words)
# TODO: create empty dict and list
dict = {}
lst = []

def word_count(t_words):
    for word in t_words:
        dict[t_words] = dict.get(t_words,0) + 1

    for key,value in list(dict.items()):
        lst.append((key,value))
    return dict

word_count(words)

1 Answer

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Just one little error. You're using t_words which is the entire tuple as opposed to the individual iteration word as per your for loop. Also, you are not returning anything from your function.

Try using this line instead and you should be working:

# change this line
dict[t_words] = dict.get(t_words,0) + 1
# to this - using word
dict[word] = dict.get(word, 0) + 1

# maybe return the list of tuples
return lst
RM Hrdr
RM Hrdr
3,491 Points

Did mess up the t_words for word and modified the line you've recommended.

Thank you! Dave, I did get the code to work in the REPL, however, it's not working in this challenge.

In REPL

Blackbox:coding-challenges rmph$ python -i wordcount.py
>>> word_count(dict)
{}

>>> word_count(words)
{'i': 2, 'do': 1, 'not': 1, 'like': 1, 'it': 1, 'sam': 1, 'am': 1}

Error in challenge

Bummer: Hmm, didn't get the expected output. Be sure you're lowercasing the string and splitting on all whitespace!

when I print the lines are all in lowercase and used .split()

>>> words = "I do not like it Sam I Am".lower().split()
>>> print(words)
['i', 'do', 'not', 'like', 'it', 'sam', 'i', 'am']

can you help me figure out what I've missed?

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Ah, thought you were just working with tuples for the fun of it as opposed to working the challenge. What are you submitting - the prompting and stuff outside the function will mess it up? Have you tested the actual output from the function?

If you just submit the following it will pass:

def word_count(t_words):
    dict = {}
    t_words = t_words.lower().split()
    t_words = tuple(t_words)
    for word in t_words:
        dict[word] = dict.get(word, 0) + 1

    return dict

Testing:

# trying
print(word_count("test words I do not like it sam i am  test   words"))

#outputs
{'test': 2, 'words': 2, 'i': 2, 'do': 1, 'not': 1, 'like': 1, 'it': 1, 'sam': 1, 'am': 1}
RM Hrdr
RM Hrdr
3,491 Points

That worked flawlessly, thank you for your awesome help Dave!

What are you submitting - the prompting and stuff outside the function will mess it up? Have you tested the actual output from the function?

Initially, yes it was the prompting but realised that no one was sitting at a desktop to enter any input so I refactored the code in my IDE. I've also tested the output from the function and it seems that the word count is iterating every execution because I've kept on passing values to the function.