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

Lucas Andrews
Lucas Andrews
4,919 Points

NameError: name 'add_entry' is not defined

I've attempted to reproduce diary.py exactly as it is in the video, but when I run it, instead of printing the menu options, it gets stuck on line 11, where I'm preparing the OrderedDict. The full error is:

Traceback (most recent call last):
File "./diary.py", line 11, in <module>
('a', add_entry),
NameError: name 'add_entry' is not defined

Towards the top of my diary.py file, I have the following code:

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

And towards the bottom, I have this code:

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

Any idea what's happening here? Thanks.

Can you paste in the code that is raising the error?

Lucas Andrews
Lucas Andrews
4,919 Points

Please see above as I've updated the question with additional details. Thanks.

1 Answer

Your problem is that add_entry is not defined, like the error says. (Errors are usually right.) A few things to check is that you are importing add_entry or that you are defining it before the OrderedDict. Python needs to have the name before you can put it in somewhere. If you are importing it you will need to define it somewhere else.

If you're doing one of those two things it should work.

Lucas Andrews
Lucas Andrews
4,919 Points

Thanks, Jacinator - I can see it, now. I just reviewed the video http://goo.gl/ofY7nH too and in fact, towards the end, the teacher moved the code below the functions to fix this very error.