1
我试图在运行其他进程时运行后台计时器。我已经使用thread.Thread:线程进程等待输入
import threading
import random
import time
import sys
def timer(limit):
time.sleep(limit)
sys.exit()
def code():
dictionary=['hello','loading','dumb']
word=[]
c=random.choice(dictionary)
answer=c
for x in c:
word.append(x)
y=0
words=''
while y!=len(c):
x=random.choice(word)
word.remove(x)
words=words+x
y+=1
print(words)
guess=input()
if guess==answer:
print('NOICE')
else:
print("NUB")
permit=input('Play Again?')
t1=threading.Thread(target=timer, args=(10,))
t2=threading.Thread(target=code)
t1.start()
t2.start()
这就是我所有的代码。问题是,当我运行它时,计时器在退出之前等待输入。无论输入是否输入,我都希望计时器以十秒的时间退出。我认为线程化会使程序不必等待对方。请帮助? 哦,我只有几天进入python,所以请保持你的解释简单。谢谢。
如果你不使用Windows,你可能会发现一些有用的答案[here](http://stackoverflow.com/questions/1335507/keyboard-input-with-timeout-in-python)。 – John