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 Using Databases in Python Our Diary App Switching It Up

Dara Roberts
PLUS
Dara Roberts
Courses Plus Student 7,770 Points

Name error persisting after moving OrderedDict to the bottom of file

After moving the OrderedDict to the bottom of the file

menu = OrderedDict([
    ('a', add_entry),
    ('v', view_entry),
])

if __name__ == '__main__':
    initialize()
    menu_loop()

My command line returns this error:

Action: a
Traceback (most recent call last):
  File "diary.py", line 56, in <module>
    menu_loop()
  File "diary.py", line 31, in menu_loop
    choice = input('Action: ').lower().strip()
  File "<string>", line 1, in <module>
NameError: name 'a' is not defined

3 Answers

Ah, apparently this is an issue pre Python 3.x, Python2's input read and evaluates what you put into it, in this case looking to return a variable we put into the input() , in python2 you would need to do raw_input(), python3 does not have this behavior

I have the same issue, is there anyone who can explain why?

Dara Roberts
PLUS
Dara Roberts
Courses Plus Student 7,770 Points

Yeah I completely forgot to respond to my own question by mentioning the switch to python 3 that helped fix it. Sorry!