如何在Python中运行后台计时器

如何在Python中运行后台计时器,python,time,timer,while-loop,Python,Time,Timer,While Loop,我目前正在制作一个数学游戏,用户有60秒的时间回答尽可能多的问题。到目前为止,除了计时器外,我的一切都正常工作,计时器要么倒计时到0,要么倒计时到60,然后停止游戏。现在,我将计时器设置为time.clock(),以计数到60,当计时器小于60时,游戏将继续运行。然而,由于某种原因,time.clock()并没有像我期望的那样工作。我还试着同时运行两个while循环,但都不起作用。有人能帮我吗?只需寻找一种在后台运行计时器的方法 这是我的密码: score = 0 timer =

我目前正在制作一个数学游戏,用户有60秒的时间回答尽可能多的问题。到目前为止,除了计时器外,我的一切都正常工作,计时器要么倒计时到0,要么倒计时到60,然后停止游戏。现在,我将计时器设置为time.clock(),以计数到60,当计时器小于60时,游戏将继续运行。然而,由于某种原因,time.clock()并没有像我期望的那样工作。我还试着同时运行两个while循环,但都不起作用。有人能帮我吗?只需寻找一种在后台运行计时器的方法

这是我的密码:

    score = 0
    timer = time.clock()
    lives = 3

    while timer < 60 and lives > 0:
        if score >= 25:
            x = random.randint(-100,100)
            y = random.randint(-100,100)
            answer = int(raw_input("What is %d + %d? " % (x,y)))
            if answer == x + y:
                print "Correct!"
                score += 1
            else:
                print "Wrong!"
                lives -= 1
        elif score >= 20:
            x = random.randint(-75,75)
            y = random.randint(-75,75)
            answer = int(raw_input("What is %d + %d? " % (x,y)))
            if answer == x + y:
                print "Correct!"
                score += 1
            else:
                print "Wrong!"
                lives -= 1
        elif score >= 15:
            x = random.randint(-50,50)
            y = random.randint(-50,50)
            answer = int(raw_input("What is %d + %d? " % (x,y)))
            if answer == x + y:
                print "Correct!"
                score += 1
            else:
                print "Wrong!"
                lives -= 1
        elif score >= 10:
            x = random.randint(-25,25)
            y = random.randint(-25,25)
            answer = int(raw_input("What is %d + %d? " % (x,y)))
            if answer == x + y:
                print "Correct!"
                score += 1
            else:
                print "Wrong!"
                lives -= 1
        elif score >= 5:
            x = random.randint(-10,10)
            y = random.randint(-10,10)
            answer = int(raw_input("What is %d + %d? " % (x,y)))
            if answer == x + y:
                print "Correct!"
                score += 1
            else:
                print "Wrong!"
                lives -= 1
        elif score >= 0:
            x = random.randint(-5,5)
            y = random.randint(-5,5)
            answer = int(raw_input("What is %d + %d? " % (x,y)))
            if answer == x + y:
                print "Correct!"
                score += 1
            else:
                print "Wrong!"
                lives -= 1
    if lives == 0:
        print "Oh no! You ran out of lives! Your score was %d." % score
    elif timer == 60:
        print "Time's up! Your score is %d." % score
else:
    print "Goodbye!"
score=0
计时器=time.clock()
生命=3
当计时器<60且寿命>0时:
如果得分>=25:
x=random.randint(-100100)
y=random.randint(-100100)
答案=int(原始输入(“什么是%d+%d?”%(x,y)))
如果答案==x+y:
打印“正确!”
分数+=1
其他:
打印“错!”
寿命-=1
elif分数>=20:
x=random.randint(-75,75)
y=random.randint(-75,75)
答案=int(原始输入(“什么是%d+%d?”%(x,y)))
如果答案==x+y:
打印“正确!”
分数+=1
其他:
打印“错!”
寿命-=1
elif分数>=15:
x=random.randint(-50,50)
y=random.randint(-50,50)
答案=int(原始输入(“什么是%d+%d?”%(x,y)))
如果答案==x+y:
打印“正确!”
分数+=1
其他:
打印“错!”
寿命-=1
elif分数>=10:
x=random.randint(-25,25)
y=random.randint(-25,25)
答案=int(原始输入(“什么是%d+%d?”%(x,y)))
如果答案==x+y:
打印“正确!”
分数+=1
其他:
打印“错!”
寿命-=1
elif分数>=5:
x=random.randint(-10,10)
y=random.randint(-10,10)
答案=int(原始输入(“什么是%d+%d?”%(x,y)))
如果答案==x+y:
打印“正确!”
分数+=1
其他:
打印“错!”
寿命-=1
elif分数>=0:
x=random.randint(-5,5)
y=random.randint(-5,5)
答案=int(原始输入(“什么是%d+%d?”%(x,y)))
如果答案==x+y:
打印“正确!”
分数+=1
其他:
打印“错!”
寿命-=1
如果寿命=0:
打印“哦,不!你的生命耗尽了!你的分数是%d.%”
elif定时器==60:
打印“时间到了!您的分数是%d.%”
其他:
打印“再见!”
使用
time.time()
,它返回历元时间(即自1970年1月1日以来的秒数)。您可以将其与开始时间进行比较,以获得秒数:

start = time.time()
while time.time() - start < 60:
    # stuff
这定义了一个引发异常并在超时发生时调用的函数。现在,您可以将while循环放入try-catch块并设置计时器:

signal.alarm(60)
try:
    while lives > 0
        # stuff
except:
    # print score

每次要检查时,都需要
time.clock()
。另外,time.clock()并不像你想象的那样。这个问题有很好的答案:所以设置timer=time.time()并将我的while循环更改为:while time.time()-timer<60,lives>0?它成功了!谢谢你,迈克。然而,出现了另一个问题。现在,如果命令行中有问题,游戏将不会退出,无论时间>60。只有在问题得到回答后,游戏才会停止。有没有办法立即停止游戏?是的,你可以中断其他进程,但它有点复杂,见上文。
signal.alarm(60)
try:
    while lives > 0
        # stuff
except:
    # print score