# Calculating time differences inside a While loop

Here is my issue:
the following code produces a random 2 digit number and ask the user to figure out the square of the number. It then suppose to tell the user how long it took the user (in seconds) to find the solution.
So I have start_time which starts as soon as the question appears on the screen
when the user guesses wrong it asks to try again, and when it is finally right it tells it how long it took the user to come up with the right answer.
My issue : When I start with wrong guesses and then get it right, the time calculation is working fine.
If I get the right answer the first time, I get zero as the time difference, The end_time becomes equal to the start_time.
will appreciate any help !

``````import random
import time
#
#
def square():
number = random.randint(0, 101)
result = number * number
flag = 1
test = int(input('what is the suqare of {}: ? ' .format(number)))
start_time = time.time()
while flag == 1:
print('start start time is {}'. format (start_time))
if test == result:
end_time = time.time()
print('end_time is {}'. format(end_time))
flag = 0
else:
test = int(input('try again'))

elapsed_time = end_time - start_time
print('you got it!! the asnwer is {} , it took you {} sconds to calculate'. format(test, elapsed_time))

square ()
#
``````

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.