0

In python, would the best way to keep a loop running until stopped by the user be as follows?

while not KeyboardInterrupt:

# The code I want repeated until I specify it to stop

break

I am new to code and would appreciate the help.

G.Morgan
  • 1
  • 2
  • 4
    Possible duplicate of [How to kill a while loop with a keystroke?](http://stackoverflow.com/questions/13180941/how-to-kill-a-while-loop-with-a-keystroke) – Priyansh Goel May 01 '16 at 12:25

2 Answers2

1

How to kill a while loop with a keystroke?

Taken from Keith's answer

try:
    while True:
        do_something()
except KeyboardInterrupt:
    pass

I suggest that you read up on exceptions and exception handling so you undertand what is different between the above and your posted code

Community
  • 1
  • 1
user3252497
  • 121
  • 4
  • There is no point in answering a duplicate with an answer that was in the dupe target. You can instead, flag it as a duplicate and it will probably be closed with a link to the other question. – zondo May 01 '16 at 12:38
0
while True:
//do your thing

This would help

Suever
  • 64,497
  • 14
  • 82
  • 101
Priyansh Goel
  • 2,660
  • 1
  • 13
  • 37