Last active
February 21, 2017 12:01
-
-
Save wtrssn/086092ad79a2d9af7020688e6f96f723 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from time import sleep, time | |
total_reaction_time=0 | |
for i in range(5): | |
print('Run number: ', i+1) | |
print('Ready...') | |
sleep(5) | |
start = time() | |
print('Quick. Press the ENTER key') | |
input() | |
reaction_time = time() - start | |
total_reaction_time=total_reaction_time + reaction_time | |
print('you took ',reaction_time,'seconds') | |
print('Your average time was ',total_reaction_time/(i+1)) | |
''' | |
EXPLANATION ---------------------------- | |
Here we will import two methods (in the programming of object orientations are called methods, in normal slang | |
are called functions) the methods are "sleep ()" and "time ()". These are inside the library "time.py" (it is a | |
file that by default is included in the installation of Python 3)... | |
def sleep(seconds): # real signature unknown; restored from __doc__ | |
""" | |
sleep(seconds) | |
Delay execution for a given number of seconds. The argument may be | |
a floating point number for subsecond precision. | |
""" | |
pass | |
def time(): # real signature unknown; restored from __doc__ | |
""" | |
time() -> floating point number | |
Return the current time in seconds since the Epoch. | |
Fractions of a second may be present if the system clock provides them. | |
""" | |
return 0.0 | |
... those are your statements, as they appear in the file time.py | |
Now it is not important that you understand all of these statements, the important thing is that you know that | |
we have only imported those functions and not all that the "time" library offers; This is a good practice to | |
save ram. | |
If you are going to make intensive use of all methods of the library "time" the import code would look like this: | |
from time import * | |
The method "sleep" you see in your statement that must be called by entering a parameter enter parenthesis, | |
that parameter are the seconds that we want the application to stop, ie sleep paralyzes the execution of the | |
program x seconds, ie the lines of code after Sleep will not run until the x time that we entered as a parameter | |
has passed. | |
The method "time" has no parameters, what it does is to return a value, that value is the time (in seconds) | |
that the operating system (windows, linux, mac os) has at that moment in which the method has been called. | |
If you try to put the following instruction you will see that it prints a very long number: | |
Print (time ()) | |
1487672375.934989 | |
''' | |
from time import sleep, time | |
''' | |
"Total_reaction_time" is a variable whose name we have invented. It is not part of the reserved terms of the | |
Python 3 syntax, also called "Python 3 keywords" (see more in: https://www.programiz.com/python-programming/keyword-list) | |
This variable is initialized with a value of zero 0, this is what is called declaring a variable, so that it can | |
later be used, try to eliminate the following statement: | |
Total_reaction_time = 0 | |
You will see that when you run the program it gives you the following error: | |
Traceback (most recent call last): | |
File "/Volumes/JOB/Documents/PYTHON/pygame/001.py", line 67, in <module> | |
total_reaction_time=total_reaction_time + reaction_time | |
NameError: name 'total_reaction_time' is not defined | |
''' | |
total_reaction_time=0 | |
''' | |
Now we will deal with the "for" loop and the "range" object. | |
The range object (5) creates a range of values that are 0, 1, 2, 3, 4 ... yes you will be wondering why the devils | |
start by 0 and not by 1 (things of the Iformatión Technology :-) | |
"For i in range (5):" what it does is to perform an iteration for each value that gives range (5), that is, it | |
goes through the values one by one and for each iteration, we can tell it to do something, in this case Which will | |
make the program is the following two lines of code, which appear after the two points ":", ie: | |
Print ('Run number:', i + 1) | |
Print ('Ready ...') | |
start = time() | |
print('Quick. Press the ENTER key') | |
input() | |
reaction_time = time() - start | |
total_reaction_time=total_reaction_time + reaction_time | |
print('you took ',reaction_time,'seconds') | |
The two points are very important because they tell Python that there is the iteration of running through values | |
with the "for" loop, | |
You will ask why only those lines, and not all of them that go next, because of the indentation, that is to | |
say the 4 blank spaces that are before each one of those two lines, in other programming languages are usually | |
enclosed Between brackets "{}", but Python does with the indentation. You can imagine that Python is a language | |
that is very orderly, but if you mistakenly erase a space, the program will give you an error. | |
The final instruction ... | |
print ('Your average time was', total_reaction_time / (i + 1)) | |
... is without indentation (without 4 blank spaces at the beginning) so it will run out of the iterations of the for loop. | |
The variable "i" is also invented, we are going to use it to save each of the values that we offer "range (5)" | |
when running it. | |
The statement "print ('Run number:', i + 1)" will print the phrase "Run number:" followed by the value with "i" | |
adding 1 more. If you try the following statement: | |
Print ("a", 1, "b", 33, "c") | |
At 1 b 33 c | |
You will see that "print" displays everything you enter separated by commas. | |
''' | |
for i in range(5): | |
print('Run number: ', i+1) | |
print('Ready...') | |
''' | |
Stop the program, for 5 seconds none of the following code instructions will be executed | |
''' | |
sleep(5) | |
''' | |
"Start" is another variable that we have invented, and in which we save the time in seconds that the system | |
offers us through the method time () | |
''' | |
start = time() | |
print('Quick. Press the ENTER key') | |
''' | |
"Input" is a python method that waits for the user to enter text by the terminal, all the text, until we | |
press the intro key. | |
In this case we are not going to do anything with the text, we only use the call to "input ()" to stop | |
the program until the user presses the intro key. Like "sleep (5)" the "input" method stops the execution | |
of any subsequent instruction. | |
You could try the following instructions: | |
a = "" | |
a = input ("tell me something") | |
print (a) | |
''' | |
input() | |
''' | |
Now we calculate the reaction time, ie the time it took to press the intro key, and save it in the variable | |
"reaction_time". | |
reaction_time = "current time in seconds" minus "the time we get in the start variable" | |
The statement "#print ('that was fast')" is not executed, it is commented, if you want a line of code | |
not to be executed in python, just put the character "#" at the beginning of the line. Code comments are good | |
practice in the development world and are usually used to comment the code, and make others understand it. | |
''' | |
#print('that was fast') | |
reaction_time = time() - start | |
''' | |
The instruction ... | |
Total_reaction_time = total_reaction_time + reaction_time | |
... serves to accumulate in the variable "total_reaction_time" all the reaction times, until the end of the | |
iteration of the bubcle for, the instruction would read like this: | |
The value of the NEW variable total_reaction_time will be equal to the sum of the value of the previous | |
variable total_reaction_time (previous iteration of the for loop) plus the new reaction time saved in the | |
variable reaction_time | |
''' | |
total_reaction_time=total_reaction_time + reaction_time | |
print('you took ',reaction_time,'seconds') | |
''' | |
The final instruction | |
Print ('Your average time was', total_reaction_time / (i + 1)) | |
... it has no indentation (without 4 blank spaces at the beginning) so it will run out of the iterations of | |
the for loop. | |
What it does is take the sum of all the reaction times and divide by 5, to extract the average reaction time. | |
Remember that the last value of "i" when leaving the for loop is 4, 4 + 1 = 5 | |
''' | |
print('Your average time was ',total_reaction_time/(i+1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment