Reputation: 808
I got the follow code. I have added time() to it to let me calculate the delay between when the user get's the problem and when the user inputs the data.
But the resulting time is not always correct and I am not sure why.
from time import time
from random import randint
a = randint(0,1000)
b = randint(0,1000)
cor_answer = a+b
usr_answer = int(input("what is "+str(a)+"+"+str(b)+"? \n"))
start = time()
if usr_answer == cor_answer:
print("yes you are correct!")
else:
print("no you are wrong!")
end = time()
elapsed = end - start
print("That took you "+str(elapsed)+" seconds. \n")
This is the result executing from cmdline:
~/math_quiz/math_quiz$ python3 math_quiz.py
what is 666+618?
1284
1284
1284
yes you are correct!
That took you 4.291534423828125e-05 seconds.
But time() clearly works because if I run it in IDLE I get this:
>>> start = time()
>>> time()-start
13.856008052825928
So I am not sure why the execution from cmdline gets me a different result.
Thanks
Upvotes: 0
Views: 53
Reputation: 405
Your code currently start the timer after user input the answer
You need to put this code start = time()
before usr_answer = int(input("what is "+str(a)+"+"+str(b)+"? \n"))
Upvotes: -1