Wie starte ich einen Hintergrund-Timer in Python?

Ich mache gerade ein Mathe-Spiel, in dem der Benutzer 60 Sekunden Zeit hat, um so viele Fragen wie möglich zu beantworten. Ab sofort läuft alles bis auf den Timer, der entweder auf 0 oder auf 60 herunterzählt und dann das Spiel stoppt. Im Moment habe ich den Timer auf time.clock () gestellt, um bis zu 60 zu zählen, und während der Timer kürzer ist, läuft das Spiel weiter. Aus irgendeinem Grund funktioniert time.clock () jedoch nicht so, wie ich es erwartet habe. Ich habe auch versucht, zwei while-Schleifen gleichzeitig auszuführen, was auch nicht funktioniert hat. Kann mir hier jemand helfen? Einfach nach einer Möglichkeit suchen, einen Timer im Hintergrund laufen zu lassen.

Hier ist mein Code:

    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!"

Antworten auf die Frage(1)

Ihre Antwort auf die Frage