Using Spyder
on windows10
.
This keeps running infinitely when one might expect the code to exit
if i>3
:
import time
i = 0
while True:
i+= 1
print('hello')
time.sleep(1)
if i > 3:
exit()
quit()
So neither the quit()
nor exit()
functions work.
However, this does exit
when i>3
:
import sys
import time
i = 0
while True:
i+= 1
print('hello')
time.sleep(1)
if i > 3:
exit()
quit()
sys.exit()
It is noted that the break
function works, but the requirement is for the code to terminate (as opposed to breaking out of the loop and continuing).
It is also noted from this question (Code still executes after "quit" or "exit", Python, Spyder) that this is a spyder
issue.
Why is this the case and, given the above, what is the best way to exit a while loop preferably without requiring an import
?