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

Gilang Ilhami
Gilang Ilhami
12,045 Points

Unexpected error in creating diary.py

Something unexpected error has occurred , but i don't understand what's going on

Enter 'q' to quit                                                                     
a) str(object='') -> str                                                              
str(bytes_or_buffer[, encoding[, errors]]) -> str                                     

Create a new string object from the given object. If encoding or                      
errors is specified, then the object must expose a data buffer                        
that will be decoded using the given encoding and error handler.                      
Otherwise, returns the result of object.__str__() (if defined)                        
or repr(object).                                                                      
encoding defaults to sys.getdefaultencoding().                                        
errors defaults to 'strict'.                                                          
v) str(object='') -> str                                                              
str(bytes_or_buffer[, encoding[, errors]]) -> str                                     

Create a new string object from the given object. If encoding or                      
errors is specified, then the object must expose a data buffer                        
that will be decoded using the given encoding and error handler.                      
Otherwise, returns the result of object.__str__() (if defined)                        
or repr(object).                                                                      
encoding defaults to sys.getdefaultencoding().                                        
errors defaults to 'strict'.                                                          
Action:                                                                               

When i tried to continue and enter 'a', this happens

Action: a                                                                             
Traceback (most recent call last):                                                    
  File "./diary.py", line 51, in <module>                                             
    menu_loop()                                                                       
  File "./diary.py", line 31, in menu_loop                                            
    menu[choice]()                                                                    
TypeError: 'str' object is not callable

diary.py

#!/usr/bin/env python3
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():
    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__))
        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():
    """Delete an entry"""

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

])

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

When you are creating your 'menu' ordered dictionary, you are using strings instead of calling the functions. This what is causing the errors, changing the code to the following ought to fix your problem.

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