How can i loop code until a user input changes a variable in python

futurelucas4502 :

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.

finefoot :

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")

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=297633&siteId=1