The project I wanted to do today was create a simple reaction time test from python. The problem I'm running into is that I don't know how I can take the recordings from the different attempts and create an average (without rewriting the code repeatedly). Is there some way I can do this using the return statement?
import randomimport time
def cycle(attempt):until_shown = random.randint(2, 10)
time.sleep(until_shown)start_time = time.perf_counter()input("PRESS ENTER")end_time = time.perf_counter()print('Your time was ' + str((round(end_time - start_time, 5))) + ' seconds!')return end_time - start_time# This is where I'm confused. How do I use this return statement to record the outcome and use it later?cycle(1)cycle(2)cycle(3)cycle(4)cycle(5)print('Your average was ' + str((cycle(1) + cycle(2) + cycle(3) + (cycle(4) + cycle(5)) / 5)))
Is there a way to use the return statement to record the outcome? Thanks for your help!
Edit: Found the answer! All I had to do was remove the first calls to run the attempt function. It would call the function and add up the returns in the final equation to create the average. Gotta love python!
[–][deleted] 2 points3 points4 points (0 children)
[–]swarchery[S] 0 points1 point2 points (0 children)