我想每5秒钟重复执行一次功能,并同时从用户那里获取输入,并基于输入停止执行吗? 例如:
def printit():
t=threading.Timer(3.0,printit)
t.start()
n=str(input())
if(n=='rajesh'):
t.cancel()
else:
#I want to continue the execution here
答案 0 :(得分:0)
这应该有帮助
import time
#use a While loop
While True:
#request said user input
x= input("Please Press 1 to continue Or 2 to Exit")
#then an if statement
if x==1:
#call your function
printit()
time.sleep(5)
else:break
这应该可以解决问题
答案 1 :(得分:0)
如果您真的想使用线程,那应该可以:
import threading
import time
def worker():
while True:
user_input = input("Enter text:")
if user_input == 'rajesh':
break
else:
time.sleep(5)
thread = threading.Thread(target=worker, daemon=True)
thread.start()
thread.join()
答案 2 :(得分:-2)
这应该有帮助
#request said user input
x= input("Please Press 1 to continue Or 2 to Exit")
#use a While loop
While True:
#then an if statement
if x==1:
#call your function
printit()
else:break
这应该可以解决问题