Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

keyboard command to break out of infinite loop?

14 views
Skip to first unread message

BartlebyScrivener

unread,
Apr 16, 2006, 3:59:24 AM4/16/06
to
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

zweistein

unread,
Apr 16, 2006, 4:58:09 AM4/16/06
to
Try CTRL + C. If it doesn't work, CTRL + Pause/Break should also work
(if you're running it from the CLI).

HTH

kodi

unread,
Apr 16, 2006, 8:08:16 AM4/16/06
to

ctrl+c

or ctrl+c+enter

BartlebyScrivener

unread,
Apr 16, 2006, 1:44:35 PM4/16/06
to
Thank you, both. I'll put in on a sticky!

rick

Scott David Daniels

unread,
Apr 16, 2006, 2:02:17 PM4/16/06
to
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
scott....@acm.org

BartlebyScrivener

unread,
Apr 16, 2006, 5:03:17 PM4/16/06
to
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

0 new messages