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.