When running commands with the interpreter, if you get stuck in a while
loop, is there a keyboard command to break out of it?
Or is the only way out a triple-finger salute and End Task?
rd
HTH
ctrl+c
or ctrl+c+enter
rick
try:
response = raw_input('Prompt: ')
except:
print 'Got here'
If you run this code and hit Control-C in response to the prompt
(guaranteeing you are inside the try-except block), you will see
a "Got here" printed. Similarly, you can look for this one exception,
and treat it specially:
try:
response = raw_input('Prompt: ')
except KeyboardInterrupt, error:
print 'Got here with "%s" (%r)' % (error, error)
Running this and entering Control-C at the prompt will show you
that the exception you get from a KeyboardInterrupt has a reasonable
repr (the result of the %r translation), but its str conversion (the
result of the %s translation) is a zero length string (which is kind
of hard to see in a print).
So, avoid "except:" when catching exceptions, and your life will be
happier.
--Scott David Daniels
scott....@acm.org
That was instructive. On my machine, it is Ctrl + Break that does it.
Ctrl + C doesn't do anything.
What I should really do is figure out roughly how many times the while
loop should run, and then put an outer limit on it, so that it will run
normally as long as it doesn't exceed x, after which it breaks and
says, "Hey, Dummy, you were headed into an infinite loop."
Could be a good exercise.
Thank you again.
rick