After the lab, you should be proficient at
Create a subdirectory called data
. Copy all
the files from /home/courses/cs111/handouts/lab7
into the
data
directory you created.
We'll practice writing several Python programs, each in their own text file. Name the files lab7.1.py through lab7.4.py.
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
One function will handle the encoding. This function takes
as parameters a string and a key and returns the encoded
string. Modify your program to use this function. Put the
driver into a main
function. Test to
make sure the program is still working correctly.
Then, modify your program to include a function called
translateLetter
that takes as parameters a
letter and a key and returns the translated version of the
letter. Call the translateLetter
function in
the encoding function you just wrote.
game.py
that contains some useful game variables and functions.
Constants:
HEADS
TAILS
Assign the constants appropriate values.
Functions:
flipCoin()
-- returns either HEADS
or TAILS
rollDie(sides)
-- returns a random value
between 1 and sides, inclusiverollMultipleDice(numDice, sides)
-- simulates
rolling multiple dice, each with the same number of sides.
Returns the total value from rolling multiple dice.Test each of the functions in a function called
main
in the game module.
game
module, i.e.,
use game
's constants and call game
's
functions.
When you import the game
module, the test code will
likely be executed. There are two ways to fix that: either (1) use that
magic line of code if __name__ == "__main__": main()
(see
an example from last Wednesday's class OR (2) comment out the function
call to main in the game module.
data/phrases.txt
file that you
copied from the handouts
directory.
Example Output:
This program will encode a whole file using Caesar ciphers. Enter the name of a file to encode: song.txt Enter an encoder key (an integer between -25 and 25): 1 The encoded file is uijt pof hpft pvu up uif pof d mpwf uijt pof hpft pvu up uif pof d mfgu cfijoe
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 scores are sorted.) Calculate the average score, as described above. Display the judges' scores and the final score.
Example input file:
5.7 8.3 9.1 8.0 8.9 8.8 8.5
Example output file:
Gymnastics Score ---------------- 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.
Example files are data/nastia.dat
and data/shawn.dat
Break this problem into pieces and tackle them one at a time.
game
module. You can add more functions to the game
module, as
appropriate. The difficulty of the game will determine the amount of
extra credit you will receive.
Since you already have a solution for Craps, writing another solution
that uses the game
module (and adding other functions, as
appropriate) will earn you 5 extra credit points.
turnin
directory. (Review
the UNIX handout if you don't remember
how to do that.)labs/lab7
directory.
Verify that you have only the .py files you wrote (plus a
directory called data
).printLab.sh
command to create a file to print out.
You should probably print from the
labs
directory.Print the file using the lpr
command introduced in
the first lab.
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 1:20
p.m. on Friday.
Ask well before the deadline if you need help turning in your assignment!