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 Gettin' CRUD-y With It View and Search Entries

Terrell Stewart
PLUS
Terrell Stewart
Courses Plus Student 4,228 Points

Which line of code switches diary entries?

After reviewing the script i'm left with a curious question. Which line/lines of code actually allow the program to switch to the next diary entry?

2 Answers

Carlos Federico Puebla Larregle
Carlos Federico Puebla Larregle
21,073 Points

In the method "view_entries()" you assign all the entries, order by the timestamp in descendent order to the variable entries. When you use the "for loop" over the entries you are actually looking in every "entry in entries" one at a time. So there's your "switch" lines. I hope that helps a little bit.

def view_entries(search_query=None):
    """View previous entries."""
    entries = Entry.select().order_by(Entry.timestamp.desc())
    if search_query:
        entries = entries.where(Entry.content.contains(search_query))

    for entry in entries:
        timestamp = entry.timestamp.strftime('%A %B %d, %Y %I:%M%p')
        clear()
        print(timestamp)
        print('='*len(timestamp))
        print(entry.content)
        print('\n\n'+'='*len(timestamp))
        print('n) next entry')
        print('d) delete entry')
        print('q) return to main menu')

        nex_action = input('Action: [Ndq] ').lower().strip()
        if nex_action == 'q':
            break
        elif next_action == 'd':
            delete_entry(entry)
Yuan Gao
Yuan Gao
8,947 Points

How come it didn't print out "every entries in entry" like if would in "for a in A: print(a)"?

Or if we want it tot print out all entries at once, how should the code look differently?

lukeliu
lukeliu
6,342 Points

Every time the FOR loop runs it automatically print the next entry in "entries". Imagine if you press n), the IF condition for q) and d) isn't activated and the current loop goes to the end and start again (This is how FOR ...IN... loop works). At the moment it runs again from the begining, the next entry is printed.