[Tutor] Stopping a loop with user input in curses
Jeff Shannon
jeff@ccvcorp.com
Mon Jul 21 18:32:01 2003
Lloyd Kvam wrote:
> The following should help:
> stdscr.nodelay(1) # do not wait for a key press
> while 1:
> t = time.asctime()
> clockWindow.addstr(1, 1, t)
> clockWindow.refresh()
> try:
> c = stdscr.getch()
> if c == ord('q'):
> curses.beep()
> break
> except <NeedExceptionName here>: # might be stdscr.error
> time.sleep(1)
This isn't quite right -- as written, the 'try:' block won't be executed
until *after* the 'while 1:' loop ends (i.e., never). To fix this, the
entire try/except segment needs to be indented to the same level as the
rest of the loop contents, instead of the level of the while statement,
i.e. :
stdscr.nodelay(1) # do not wait for a key press
while 1:
t = time.asctime()
clockWindow.addstr(1, 1, t)
clockWindow.refresh()
try:
c = stdscr.getch()
if c == ord('q'):
curses.beep()
break
except <NeedExceptionName here>: # might be stdscr.error
time.sleep(1)
This may have been caused by a mailer error or some such, but
indentation matters...
Jeff Shannon
Technician/Programmer
Credit International