gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 10:24:30 25 4
gpt4 key购买 nike

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

最佳答案

也许您甚至不需要使用异步、线程或多处理,并且可以使用 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")

关于python - 我如何循环代码直到用户输入更改 python 中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60683041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com