我如何循环代码直到用户输入更改 python 中的变量

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

我想做的是让代码不断循环,直到用户输入一些东西,例如我不希望程序不断要求用户一遍又一遍地重复输入,并且让代码循环直到用户响应输入我想我可以使用 Async IO 或 Threading 来做到这一点,但不明白如何要使用它,有人可以提供某种示例代码并解释它是如何工作的,因为我已经尝试了两者的文档但不理解它。

也许您甚至不需要使用 asyncio、线程或多处理,可以使用这个简单的解决方案 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")