Simon.Kemp
Posts: 2
Joined: Sat Mar 14, 2020 6:20 pm

same equation giving different answers

Sat Mar 14, 2020 6:35 pm

HI All.
Apologies if this has been answered somewhere else. But this has been bugging me for a couple of hours now and I can't work out why.

To start, i am quite new to python so please bear with me
I bought a pibrella and wrote the code for a reaction game.
essentially, when you react by hitting the button it prints the time you reacted in.
This was to as many decimal points as it wanted so I truncated the answer to 3 decimal places.

The start of the code being:

Code: Select all

import pibrella, time, random

def truncate(n, decimals=0):
    multiplier = 10 ** decimals
    return int(n * multiplier) / multiplier

name = input('Hi, Whats your name? ')

while True:
    time.sleep(1+(4 * random.random()))
    pibrella.buzzer.fail()
    pibrella.light.red.on()
    t=time.time()
    r=time.time()-t

    while pibrella.button.read()==0:
        time.sleep(0.001)
    print (name +', your truncated reaction time was',+ truncate(time.time()-t, 3) , 'seconds.')
    print (name +', t=',+ t)
    print (name +', time.time() =',+ time.time())
    print (name +', your reaction time was',+ time.time()-t)
    print (name +', r=',+ r)

    while pibrella.button.read()==1:
        pibrella.light.red.off()
        pibrella.light.green.on()
        time.sleep(2)
        pibrella.light.green.off()
This all works fine. However, the print outputs of

Code: Select all

time.time()-t
and

Code: Select all

r
are not the same. although they are essentially the same.

The output is as such:

Code: Select all

Hi, Whats your name? SImon
SImon, your truncated reaction time was 0.518 seconds.
SImon, t= 1584210706.946039
SImon, time.time() = 1584210707.4652462
SImon, your reaction time was 0.5192432403564453
SImon, r= 5.4836273193359375e-06
Do you want to play again? (y/n): n
Goodbye SImon. Have a nice day! :)
in my mind these 2 lines:
SImon, your reaction time was 0.5192432403564453
SImon, r= 5.4836273193359375e-06


should be the same. But they're not.
can anyone explain why?

Many thanks in advance.

Garvan
Posts: 41
Joined: Sun Jan 05, 2020 9:59 am

Re: same equation giving different answers

Sun Mar 15, 2020 7:29 am

This is an error

Code: Select all

    t=time.time()
    r=time.time()-t
Every time you call time.time() it returns the current time, so r will be equal to a tiny number.

What you should do is move r=time.time()-t down to after the button is pressed, so it is equal to the reaction time.

Simon.Kemp
Posts: 2
Joined: Sat Mar 14, 2020 6:20 pm

Re: same equation giving different answers

Sun Mar 15, 2020 8:18 am

Garvan wrote:
Sun Mar 15, 2020 7:29 am
This is an error

Code: Select all

    t=time.time()
    r=time.time()-t
Every time you call time.time() it returns the current time, so r will be equal to a tiny number.

What you should do is move r=time.time()-t down to after the button is pressed, so it is equal to the reaction time.
I've done as you suggested and it works perfectly!
Thanks you so much for pointing out my mistake. Lesson learnt!

Return to “Python”