Required Python Skills: File handling
Sample code available for educators. Get Access
In the previous lesson, you used lists to enable the Finch to record data about its environment. To store this data more permanently, you can write it to a file. For example, suppose the list tempValues stores a number of temperatures measured by the Finch. You can write those values to a file named “tempOutputFile.txt” using the code below. Notice that you need to convert each number to a string using the str() function before you can add it to the file.
Write a program which turns the Finch into a datalogger that records light, acceleration, and temperature (six values in all).
- Your program should ask the user for a length of time in seconds.
- It should then record the six data values once per second for the chosen time period.
- The program should store the data in a file.
- Each line in the file should contain six data values. The data values should be separated by commas.
- Try importing your data file into a spreadsheet program to graph your data!
In the next part of this lesson, you will use the Finch to measure a user’s reaction time and store this data in a file. The reaction time is the amount of time that it takes someone to respond to a stimulus. A stimulus is just something that happens in the person’s environment, like a beep or a change in color. As an example, try using this website to measure your visual reaction time. In this case, the visual stimulus is when the screen changes from red to green. The website measures how long it takes you to respond to this change.
Write a function isLevel() that determines whether or not the Finch is level. The function should return True if the Finch is resting level on the table, and False otherwise. This function should be able to detect even a small change from the level position.
Write a function that uses the Finch to measure a person’s visual reaction time. The visual stimulus will be the Finch’s beak changing from red to green. When the beak turns green, you will measure how long it takes the user to react by moving the Finch out of the level position.
- At the beginning of the function, ask the user to place the Finch in a level position and wait until he/she has done so.
- The Finch’s beak should turn red and wait for a random number of seconds before it changes to green. Why is this important that the timing of the stimulus be random?
- The Finch should remain level while the beak is red. If the user tries to cheat by moving the Finch while the beak is red, scold them and restart the random waiting time. How can you use your isLevel() function to do this?
- Once the Finch’s beak turns green, measure how long it takes the user to move the Finch out of the level position. How can you use your isLevel() function to do this?
- The function should return the user’s reaction time.
- Remember to give the user instructions so they know what to do!
Once you have a function to measure the visual reaction time, you should write a program that uses this function to measure 5-10 reaction times. You should save all the reaction times to a file.
Finally, your program should calculate the user’s mean reaction time, store it in the file, and print it to the screen. How does the reaction time you measure with the Finch compare to the one you measured with the website?
Create a new function to measure the user’s mean auditory reaction time. To do this, you will need to modify your reaction time function to use an auditory stimulus. How does the mean auditory reaction time compare to the mean visual reaction time?