1

I'm trying to do something in a loop (in this example print to 100) and cancel the count at any time by pressing 'Ctrl+C'.

The test code I'm using is below, however this doesn't work (EDIT: it works in a script launched from terminal but does not work when run in the python shell of my IDE - Wing 101). KeyboardInterrupt doesn't catch the 'Ctrl+C' command or any other key.

import time     

i = 1
while (i < 101):
    try:
        print '%d percent complete' % i
        time.sleep(0.5)
        i += 1
    except KeyboardInterrupt:
        break 

I've read this question here and I'm pretty sure that's not my problem because I've put the 'time.sleep(0.5)' command in to slow the program down. I've also read this question but since it's 5 years old now I assume this bug would have been fixed by now? I know that I can use threading to achieve what I want, but if only for the sake of learning I'd like to know why this method doesn't work.

I'm using python 2.7.6 in Ubuntu 14.04 and I'd appreciate any help or advice in solving this issue. (EDIT: I know the code works in isolation, but I'd still be interested in knowing why it doesn't work in the python shell of Wing 101 IDE)

EDIT 1

I've tried putting the while loop inside the try block, as suggested:

try:
    i = 1
    while (i < 101):        
        print '%d percent complete' % i
        time.sleep(0.5)
        i += 1
except KeyboardInterrupt:
    break 

But unfortunately that doesn't work either, instead I get the following error:

Traceback (most recent call last):
  File "/home/matt/autosys_repo1/python_test_scripts/test_refresh_line.py", line 13, in <module>
    break
'break' outside loop: <string>, line 13
8
  • There's a difference between your approach and an answer in the first thread you've linked. The guy suggests putting the loop sentences inside the try block. You have the while sentence outside the try/except. Have you tried doing it that way? Edit: I've tried your code and it works as expected. Commented Jul 20, 2016 at 1:21
  • Thanks for the suggestion @Brnpzs. Unfortunately that doesn't work either (see edit 1). And yes, it seems to work for everyone else so I'm confused as to why it won't work for me. I feel like its just some small detail I'm overlooking. Commented Jul 20, 2016 at 2:00
  • break has no sense outside while loop, you can use sys.exit(0) to force the exit. To do so, you need to import sys module first. Commented Jul 20, 2016 at 2:04
  • Doesn't work either I'm afraid. It just continues printing and never exits the loop until i = 100. I think the key problem is KeyboardInterrupt not recognising the key press. Commented Jul 20, 2016 at 2:08
  • Another way to solve this problem is using signal handlers , in this case, you want to handle SIGINT as shown in the answer of this question. Commented Jul 20, 2016 at 2:15

1 Answer 1

0

This is a limitation of Wing IDE 101. The only way to stop a loop like this is to restart the shell.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.