Lab 7: Practice with Files and Functions
Goals
After the lab, you should be proficient at
- using functions within your program
- refactoring your code to use functions
- writing data to files
- reading numeric data from files
- solving problems using top-down design
- practice with lists
Objective: Review
Review the slides for today's lab.
Objective: Set Up
- Set up the help client:
runHelpClient & - Copy
/csdept/courses/cs111/handouts/lab7
and all of its contents, recursively, into yourcs111
directory. This way, you don't have to create thelab7
directory first. Otherwise, you could first create thelab7
directory and then copy all of the contents of the/csdept/courses/cs111/handouts/lab7
directory (i.e., add a * to thecp
command) into yourlab7
directory.
Objective: Programming in Python
We'll practice writing several Python programs, each in their own text file.
Your programs will be graded on correctness, style, efficiency, and how well you tested them. Make sure you adhere to the good development and testing practices we discussed in class. Your code should be readable and your output should be useful and well-formatted.
After you've developed a correct solution to each program, restart
IDLE or close and reopen the IDLE "shell" by running the program again
(using F5), demonstrate that the program works using
several
- (15) Open lab7.1.py, which you copied at the beginning of lab.
Read through the functions in the file and their documentation.
Then, modify the program so that the
main
function implements what is described in its comments, e.g., draws and moves bugs, using functions defined within the program. - (15) Open
lab7.2.py
. Write a function calledsumList
that takes a list of numbers as a parameter and returns the sum of the numbers in the list. If the list has 0 elements then the function should return 0.After defining the function, test the function by calling the function several times, with a different list every time the function is called, and display the result of calling the function.
- (20) Copy the Caesar Cipher program from the last lab where you
read from a file and modify it so that it has at least three new
functions. (If your Caesar Cipher program didn't work correctly or
you want to see my version of the program, you can start
with this one.)
One function called
encodePhrase
will handle encoding a whole phrase (string). This function takes as parameters a string and a key and returns the encoded string. Modify your program to use this function. Test the encoding function, on its own, to ensure that it is working correctly.Put the driver (the rest of the code) into a
main
function. Then, test the program to make sure the whole program is still working correctly.Then, modify your program to have a function called
encodeLetter
that takes as parameters a letter and a key and returns the encoded letter. Note that this function isn't interested in how to handle non-letter characters, just letters. Call theencodeLetter
function in the encoding function you just wrote.Finally, write comments that compare the readability of the program before and after you added the functions.
- (15) Copy the previous program and modify it so that it writes
the encoded message to a file. The program should take as input the
encoded message output file's name. You may want to assume that the
output files always get written to
the
data
directory so that you don't clutter yourlab7
directory with files.The idea is that now you can encode files that you can give to others to decode (and vice versa).
Example output:
What is the name of your file to encode? phrases.txt What is the name of the file to output the encoded file to? encoded.txt What is the key to use to encode? 12 Your message has been successfully written to data/encoded.txt
Note: I broke up the large problem of encoding files using a Caesar cipher and writing the result into another file into multiple, smaller problems, each of which is easier to tackle than tackling the whole problem all at once. This is an important problem-solving skill to develop. You may feel like breaking the problem into smaller problems slows you down, but it usually doesn't because you spend less time debugging the smaller pieces than if you had tackled it all at once. You'll get lots of practice solving problems in this class--including on the next problem!
- (35)
Break this problem into pieces (what are those pieces?) and
tackle them one at a time. Use top-down design, as appropriate.
Create functions that solve each piece.
The system of scoring an Olympic gymnast is based on two separate panels of scores. The A panel judges the requirements, difficulty, and connections of a routine. The scoring starts at zero and then adds points accordingly. The B panel judges the execution of a routine, and the scoring starts at 10 with points deducted accordingly for execution and for any applicable violations such as stepping out of bounds or being over the time limit.
For each panel of judges, the lowest and highest scores are dropped (to prevent judges from biasing the results) and then averaged. The two panels are then added together for the final score. A very good score will range in the 15s and 16s.
Your task:
- Read in a file containing the gymnastics scores. (The file name can be a constant in your program.) The first line of the file is the average difficulty score. The remaining six lines are the judges' execution scores. (You cannot assume that the execution scores are sorted.)
- Calculate the average score, as described above.
- Display the judges' scores and the final score in the format
shown below.
Example input file:
5.7 8.3 9.1 8.0 8.9 8.8 8.5
Example output:
Gymnastics Scores for data/scores.dat -------------------------------------------------------- Judges Execution Scores: 8.0 8.3 8.5 8.8 8.9 9.1 Average Execution Score: 8.625 Average Difficulty Score: 5.7 The Final Score: 14.325
Your output does not need to display the scores in sorted order. However, the output should be formatted as above.
Example files are
data/nastia.dat
anddata/shawn.dat
Finishing up: What to turn in for this lab
- Copy
your
lab7
directory into yourturnin
directory. - Before printing, move the
graphics.py
file out of thelab7
directory so that you don't print this large file.Perform the following steps from your
cs111
directory.
Note that each command below links to a page with more information about using the command. - Create the printable lab assignment, using the
createPrintableLab
command:
createPrintableLab <labdirname> - View your file using the
evince
command. - Print the file using
the
lpr
command. - Log out of your machine when you are done.
Labs are due at the beginning of Friday's class. You should hand in the printed copy at the beginning of class, and the electronic version should be in the
turnin
directory before class on Friday.Ask well before the deadline if you need help turning in your assignment!
Grading (100 pts)
- Python programs: 100 pts; see above for breakdown
Have the instructor or a student assistant review your code before moving on to discuss appropriate organization and commenting.