Hi so basically I need to create a timer for this simple maths test. I need it to give each question, lets say 30 seconds for test purposes, and then move onto the next question after the time is up and repeat the timer like that. Just cant figure it out. Im new to python, any help is greatly appreciated. Here is my code

``````import random

correct = 0
incorrect = 0

for i in range(10):

number1 = random.randint(1,10)
number2 = random.randint(1,10)

question = (number1 * number2)
answer = int (input("What's %d times %d? " % (number1, number2)))

print ("That's right", name,"! Well done.\n")
correct = correct + 1
else:
print ("No, I'm afraid the answer is %d.\n" % question)
incorrect = incorrect + 1

print ("\nYou answered 10 questions.  You got %d right and %d wrong." % (correct, incorrect))

if correct >=5:
print ("Good job!")
else:
print ("Try harder next time! You got", incorrect, "answers wrong.")
``````

You would have to use multiprocessing for the timer since the program is doing two things,
2. moving on/killing the question's process after 30 seconds whether the question has or has not been answered. Multiprocessing takes a little fiddling to get the input in a thread …

## All 2 Replies

At your level it would be easier to time the seconds it takes to answer all the questions ...

``````import random
from datetime import datetime as dt

correct = 0
incorrect = 0

name = input("Enter your name: ")

start = dt.now()
for i in range(10):

number1 = random.randint(1,10)
number2 = random.randint(1,10)

question = (number1 * number2)
answer = int (input("What's %d times %d? " % (number1, number2)))

print ("That's right", name,"! Well done.\n")
correct = correct + 1
else:
print ("No, I'm afraid the answer is %d.\n" % question)
incorrect = incorrect + 1

end = dt.now()
elapsed_seconds = (end - start).total_seconds()
print("\nIt took you %d seconds to answer 10 questions" % elapsed_seconds)
print ("You got %d right and %d wrong." % (correct, incorrect))

if correct >= 5:
print ("Good job!")
else:
print ("Try harder next time! You got", incorrect, "answers wrong.")
``````

You would have to use multiprocessing for the timer since the program is doing two things,
2. moving on/killing the question's process after 30 seconds whether the question has or has not been answered. Multiprocessing takes a little fiddling to get the input in a thread since a process does not use the same input stream as the non-in-a-process code, and you might also want to communicate to/from the thread with a Manager which is another thing to be learned.`

You could also use Tkinter (probably a better choice), which has a built in "after" method. Tkinter would also enable the user to click a button when finished instead of waiting for the 30 seconds to expire.

``````""" multiprocessing example
"""

import time
from multiprocessing import Process
import os
import sys

def __init__(self):

def test_q(self, question, fileno):
## necessary to get input to work
sys.stdin = os.fdopen(fileno)  #open stdin in this process
print "\n\n", question

if __name__ == '__main__':