469,610 Members | 2,112 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,610 developers. It's quick & easy.

keyboard command to break out of infinite loop?

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

Apr 16 '06 #1
5 26876
Try CTRL + C. If it doesn't work, CTRL + Pause/Break should also work
(if you're running it from the CLI).

HTH

Apr 16 '06 #2

BartlebyScrivener wrote:
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

Apr 16 '06 #3
Thank you, both. I'll put in on a sticky!

rick

Apr 16 '06 #4
zweistein wrote:
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
sc***********@acm.org
Apr 16 '06 #5
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

Apr 16 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

43 posts views Thread by Gremlin | last post: by
1 post views Thread by Rene | last post: by
44 posts views Thread by James Watt | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.