0
There must be an easier way to interrupt loops! Or do I really have to use multi threading?
Could you please show me what I would need to add, so when a key is pressed, the infinte while loop stops but it still prints 'Finished'? while(True): print('Hello') print('Finished') Of course I could simply close the Window, or press ctrl+c, but then it would not print 'Finished' any more. I'd prefer to send an interrupt from the keyboard, which allows me to close only the infinite loop. While the following commands are still executed. I thougt about using multi threading, the actual thread which would normally be executed infinitely, and another which gets the input and tells the other thread to stop. But it has to be way more easier, doesn't it?
2 Réponses
+ 5
From Keith at stackoverflow.com:
try:
while True:
print('Hello')
except KeyboardInterrupt:
pass
print('Finished')
Then use Ctrl+C.
+ 1
Nice solution.