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)))

    if answer == question:
        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.")

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)))

    if answer == question:
        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.")

Otherwise you get into threading.

You would have to use multiprocessing for the timer since the program is doing two things,
1. getting the answer
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

class AskQuestions():
   def __init__(self):
      self.answer=None
      self.correct_answers = 0

   def test_q(self, question, fileno):
      self.answer=None
      ## necessary to get input to work
      sys.stdin = os.fdopen(fileno)  #open stdin in this process
      print "\n\n", question
      self.answer = input("What is your answer? ")
      if self.answer:
          print "     Correct", self.answer

if __name__ == '__main__':
   AQ=AskQuestions()

   for question in ["The First Thing", "Second", "Last and Least"]:
       fn = sys.stdin.fileno()
       p = Process(target=AQ.test_q, args=(question, fn))
       p.start()
       ## sleep for 10 seconds and terminate
       time.sleep(10.0)
       p.terminate()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.