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()Code: Select all
time.time()-tCode: Select all
rThe 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! :)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.