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

Returning a dictionary from a created list of an inputted string.

I can't figure out where I am going wrong here

wordcount.py
def word_count(string):
    string_copy = string[:]
    for key in (string_copy.lower()).keys():
        key_hold = key.append()
    for value in (string_copy.lower()).values():
        value_hold = value.append(len(value))

    return dict([key_hold, value_hold])

1 Answer

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Dylan,

Let's walk through your function.

string_copy = string[:]

Here you're making a copy of the input string, but if you look at the rest of your code you never modify string so there's no reason to make a copy.

    for key in (string_copy.lower()).keys():

Strings are list-like objects, not dictionary-like objects, so strings have no keys() attribute. You can check this for yourself in the Python interpreter:

>>> 'hello world'.keys()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'keys'

Similarly, they have no values() attribute:

>>> 'hello world'.values()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'values'

Note that we don't have any code written to break the string into individual words. We could accomplish that by breaking the string on any whitespace characters we find. Now, we could do this by writing a function that goes through a string and splits it up on any whitespace character it finds. But as programmers we are lazy, so let's just use the built in function that does the same thing: split()

We also need a dictionary to put the instances of the words and their counts into. Remember that we can create an empty dictionary like this:

my_dict = dict()

or like this:

my_other_dict = {}

With an empty dictionary to stuff our results into, and a function to break the string into a list of words, then you'll want to use your for loop to:

  1. go through each word;
  2. check if it is in the dictionary already;
  3. if it is in the dictionary, increase that key's value count by one;
  4. otherwise, add that word to the dictionary as a key with the value 1.

After your loop finishes, return that dictionary.

Cheers

Alex