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

Julien riera
Julien riera
14,665 Points

Quotes and choice variable

Hi everybody. I have an issue here. Actually, my code runs perfectly, but only if I enter a quoted letter, like "a". (or any other letter specified as long as I wrap it between quotes)

Here's my code :

#!/usr/bin/env python

from collections import OrderedDict
import datetime

from peewee import *

db = SqliteDatabase('diary.db')



class Entry(Model):
    content = TextField()
    timestamp = DateTimeField(default=datetime.datetime.now)

    class Meta:
        database = db


def initialize():
    """Create the database and the table if they don't exist."""
    db.connect()
    db.create_tables([Entry], safe=True)


def menu_loop():
    """Show the menu"""
    choice = None

    while choice != 'q':
        print('Enter "q" to quit.')
        for key, value in menu.items():
            print('{}) {}'.format(key, value.__doc__))  #__doc__ capture le docstring d'une fonction. Dans ce cas, il permet de le print.
        choice = input('Action: ').lower().strip()

        if choice in menu:
            menu[choice]()

def add_entry():
    """Add an entry"""


def view_entries():
    """View previous entries"""


def delete_entry(entry):
    """Delete an entry"""


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

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

If I write a, v or q without these quote marks in my input, it just returns an error. See :

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

Thanks in advance for enlighting me ! :)

3 Answers

Ali Ikram
Ali Ikram
3,056 Points

I'm having the same problem when using #!/usr/bin/env python. However, its fine when I'm using #!/usr/bin/env python3. So, I believe it something to do how python 2 and 3 takes the input.

Julien riera
Julien riera
14,665 Points

Hi, thanks for your answer.

Problem is when I put Python3, which I did in the first place, it displays an import error.

Traceback (most recent call last):
  File "./diary.py", line 7, in <module>
    from peewee import *
ImportError: No module named 'peewee'
Rick Ertl
Rick Ertl
7,278 Points

Check out this question and the teacher's answer. It helped me with the same issue since I use Python 2.7..

>>>related question