如何继续运行代码并仍然获取用户的输入,以便您可以在运行代码时在终端中通过键入来选择项目。
我尝试了这段代码:
itemA = False
itemB = False
itemC = False
while True:
input = input("input example")
if input == "a":
# execute a's code
# execute code
这只会在 input() 运行后执行 input() 下的代码,并再次询问输入。
如何使其在输入下运行代码而不为每个输入()停止?
您可以使用
threading
包来执行此操作。这基本上允许您同时运行 2 个 while
循环。就您而言,一个用于输入,一个用于其他代码。这是一些可以执行此操作的代码。
itemA = False
itemB = False
itemC = False
import threading
def execute_code():
while True:
# execute code (not waiting for input)
print("hi")
continue
threading.Thread(target=execute_code).start()
while True:
input("input example")
# execute code (waiting for input)