What I would like to do is have code loop continuously until the user enters something e.g. I don't want the program to keep asking the user for repeated inputs over and over just once and for the code to loop until the user responds to the input I think I could do this using Async IO or Threading but don't understand how to use it could someone provide some kind of sample code with an explanation of how it works as I have tried the documentation for both but don't understand it.
Asked
Active
Viewed 115 times
2
-
probably duplicated of https://stackoverflow.com/questions/20576960/python-infinite-while-loop-break-on-user-input – Edoardo Guerriero Mar 14 '20 at 13:09
1 Answers
2
Maybe you don't even need to go with asyncio or threading or multiprocessing and can use this simple solution using KeyboardInterrupt
:
from time import sleep
try:
while True:
print("Loop is active, press CTRL+C to cancel")
sleep(1)
except KeyboardInterrupt:
print("Loop cancelled")
print("Continue with other code")

finefoot
- 9,914
- 7
- 59
- 102