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 - Build a Social Network -- Macros

Hey guys,

I'm trying to build this application on my local machine & when I run the 'python app.py' command, I get

KeyError: ' '

Does anyone know what this KeyError mean?

Thanks, Chris

1 Answer

Python's built-in exception KeyError is thrown when you try to access something on a dictionary with a key that doesn't exist.

For example, if you ran the following in the python interactive shell, you would come across the KeyError exceptions as shown below:

>>> my_dict = {'val1': 'one', 'val2': 'two'}
>>> my_dict['val1']
>>> my_dict['val2']
>>> my_dict['val3']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'val3'
>>> my_dict[' ']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: ' '