如何在python程序中设置计时器
我用python编写了一个混乱的游戏,效果很好,但一旦我尝试向我的程序添加一个功能,我就找不到解决方案。。。 我想在我的程序中添加一个计时器,用于停止一切并在1分钟后完成游戏。 这是我的密码:如何在python程序中设置计时器,python,python-3.x,Python,Python 3.x,我用python编写了一个混乱的游戏,效果很好,但一旦我尝试向我的程序添加一个功能,我就找不到解决方案。。。 我想在我的程序中添加一个计时器,用于停止一切并在1分钟后完成游戏。 这是我的密码: from random import choice, shuffle import os words = ['hello','honey','green','horse','rose','window','virus','traffic','yard','camel', 'ground','tutle'
from random import choice, shuffle
import os
words = ['hello','honey','green','horse','rose','window','virus','traffic','yard','camel',
'ground','tutle','earth','internet']
count = len(words)
os.system('clear')
true_answer, false_answer = 0, 0
while(count!= 0):
word = list(choice(words))
new_word = "".join(word)
shuffle(word)
user = input(f'Can you guess what this word is ({" ".join(word)}) ? ')
if new_word == user:
true_answer += 1
else:
false_answer += 1
count -= 1
words.remove(new_word)
os.system('clear')
print(f'true answers : {true_answer}\nwrong answers : {false_answer}')
我希望玩家只玩1分钟,然后才能看到结果。使用datetime库,获取开始时的时间,并循环检查时间是否结束,下面是一个示例 你也能做到
导入时间
startTime=time.time()
#当你想开始测量时间的时候
.....
#循环进行
如果(time.time()-startTime>60):#60s
打印(“时间已过”)
谢谢,但有一个问题:当程序希望播放机键入输入时,时间暂停。。。我希望它能够工作,无论用户是否提供输入。