i am making a script that asks mathematical questions and i was wondering, how would i make a timer? I want to make a timer such that the user has 20 seconds to answer and when the time is up then to say: "Sorry, incorrect." I have tried recording the time when the question is given then subtracting it from the time they answer the question then if its great or equal to 20 to display incorrect, however it doesn't work. Any help will be greatly appreciated.
if op != '*': # != means 'not equal'
r1 = random.randint (-1, 100) # Randomises a number from -1 to 100
r2 = random.randint (-1, 100)
answer = ops.get(op)(r1,r2)
start = time.time()
equ = int(input('Q{}: What is {} {} {}?\n'.format(q, r1, op, r2)))
end = time.time()
if end - start >= 20:
print ("Sorry you took too long to answer.")
elif answer.upper() = 'FRIDAY':
print 'Correct, today is {}'.format(answer)
else:
r1 = random.randint(-1,12)
r2 = random.randint(-1,12)
answer = ops.get(op)(r1,r2)
equ = int(input('Q{}: What is {} x {}?\n'.format(q, r1, r2)))
if equ == answer :
score += 1
print("Correct, Well done, your score is: " + str(score))
else:
print("Incorrect, Sorry. Your score is: " + str(score))