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

Hi, there everyone. Why Am I not getting the response to my actions in my defined methods?

https://teamtreehouse.com/workspaces/33621432

#!/usr/bin/env python3

import datetime
import sys
from collections import OrderedDict

from peewee import *

db = SqliteDatabase('dairy.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__))
        choice = input("Action: ").lower().strip()

    if choice in menu:
        menu[choice]()


def add_entry():
    """ Add an entry. """
    print("Enter your entry, press ctrl+D when finished.")
    data = sys.stdin.read().strip()

    if data:
        if input("Save entry ? [Yn] ").lower() != "n":
            Entry.create(content=data)
            print("Saved successfully")


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()
Chris Freeman
Chris Freeman
Treehouse Moderator 68,468 Points

The workspace URL cannot be shared directly. Instead use the Snapshot icon in the upper right of the workspaces window to create a snapshot, then share the link to the snapshot. Thx.

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

Ah. Think I've spotted it. while choice loop has no exit other than "q". Since the if choice execution is outside the while loop it will only execute when a "q" is entered, but at that point, there isn't a "q" in the menu to execute.

    while choice != "q":
        print("Enter q to quit !")
        for key, value in menu.items():
            print("{}) {}".format(key, value.__doc__))
        choice = input("Action: ").lower().strip()

        # Indent if block to be inside of while loop
        if choice in menu:
            menu[choice]()

Post back if you have more questions. Good luck!!

Hi Thanks for your reply, But I had figured it out long time back, But I wanna ask you something After learning python and flask I'm currently on Django track, Would it help me to get a job in Tech, Can you show me A path and how should I approach towards it.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,468 Points

Learning Django is helpful in two ways: it shows that you can work in a complicated framework, and some jobs are specifically Django based jobs. Many jobs revolve around one library or another so being able to incorporate using a library into a project shows your adaptability and ease of learning new tools. If you are still early in your learning, it is highly recommended to develop portfolio projects to showcase what you have learned. Getting a job comes down to demonstrating that you already know something or that you have the ability to learn it quickly. Keep at it. You'll get there! Best of luck!

Thank you so much!