-4

I have created a quiz and I would like to create a timer so that it displays how long the user has taken for each question? I want to implement the timer beside each question but I can't find help anywhere as I want the timer to reset after each question

Amir
  • 10,600
  • 9
  • 48
  • 75

2 Answers2

2

The simplest way to time how long it takes to do something is take the time before you do it and take it away from the time after you do it.

In python, there is a time module. time.time() gives you how many seconds it has been since the epoch (1 January 1970).

import time

start = time.time()  # Time at the beginning of the script
main()               # What you want to time
end = time.time()    # Time at the end

print("You took", int(end - start), "seconds!")

You can even do it for multiple things at a time and store the timings in a list. The total time taken would be the list's sum.

Artyer
  • 31,034
  • 3
  • 47
  • 75
0

You could use sleep Like,

print "what's 3+2?"

Time = 0

Correctanswer = 5

while Time <= 10:

    if Time <= 10:
        Time += 1
        sleep(1)

    answer = raw_input ("")

    if answer == Correctanswer:
        Time = 11
        print "you got the right answer"

    if answer == Correctanswer or time > 10:
        break
        print "what's 8 +2?"



Time = 0

Correctanswer = 10

while Time <= 10:

    if Time <= 10:
        Time += 1
        sleep(1)

    answer = raw_input ("")

    if answer == Correctanswer:
        Time = 11
        print "you got the right answer"

Sorry if this doesn't work, I came up with it on the spot

Artyer
  • 31,034
  • 3
  • 47
  • 75