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 trialTerrell Stewart
Courses Plus Student 4,228 PointsWhich 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
21,074 PointsIn 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)
lukeliu
6,342 PointsEvery 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.
Yuan Gao
8,947 PointsYuan Gao
8,947 PointsHow 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?