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
Asked
Active
Viewed 166 times
-4
-
Could you share some of the code you've written? It's hard for us to help without that. – RevanProdigalKnight Jan 06 '16 at 21:05
-
seriously?! what language? what code do you have so far? what have you tried? – Ben Glasser Jan 06 '16 at 21:05
-
The `time` module contains functions you can use. `time.time()`, and on Py3.3+, `time.perf_counter()`. See https://docs.python.org/3/library/time.html – BrianO Jan 06 '16 at 22:28
-
related: [Measure time elapsed in Python?](http://stackoverflow.com/q/7370801/4279) – jfs Jan 07 '16 at 04:41
2 Answers
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
-
This wouldn't work, as when you sleep / raw_input, it stops and doesn't continue untill x time / input is received. Your current script will sleep for a second, ask the question and then do it again 10 times. – Artyer Jan 07 '16 at 19:14
-