keyboard command to break out of infinite loop?

B

BartlebyScrivener

Running Python on Win XP.

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
 
Z

zweistein

Try CTRL + C. If it doesn't work, CTRL + Pause/Break should also work
(if you're running it from the CLI).

HTH
 
K

kodi

BartlebyScrivener said:
Running Python on Win XP.

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



ctrl+c

or ctrl+c+enter
 
S

Scott David Daniels

zweistein said:
Try CTRL + C. If it doesn't work, CTRL + Pause/Break should also work
(if you're running it from the CLI).

HTH
Note: this will raise a KeyboardInterrupt exception, which you might
inadvertently be catching. If you have been lazy and written:
try:
<watched code>
except:
<some code>
You may get to <some code> if the Control-C (or Control-Break) is
processed in the <watched code> section. This is one reason we always
urge people to be specific about the exceptions they expect. The
following code demonstrates what is happening:

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
(e-mail address removed)
 
B

BartlebyScrivener

Thank you, Scott. I'm still learning my exceptions (obviously, or I
wouldn't get stuck in a while loop ;).

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top