Lab 7: Practice with Functions, Modules, and Files

Goals

After the lab, you should be proficient at

  1. organizing your code using functions
  2. creating and using your own modules
  3. reading and processing data from files

Linux

As usual, create a directory for the programs and output you develop in this lab.

Create a subdirectory called data. Copy all the files from /home/courses/cs111/handouts/lab7 into the data directory you created.

Objective: Programming in Python

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 good test cases, and save the output to a file named lab7.x.out, where x is the problem number.

  1. (25) Copy the Caesar Cipher program from the last lab and modify it so that it has at least two new functions. (If your Caesar Cipher program didn't work exactly right or you want to see my version of the program, you can start with this one.)

    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.

    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.

  2. (25) Create a Python script/module called game.py that contains some useful game variables and functions.

    Constants:

    Assign the constants appropriate values.

    Functions:

    Save rainbowdice.py as lab7.2.py and modify it to use the game module, i.e., use game's constants and call game's functions.

    In comments, compare the readability of rainbowdice, with and without the module.

  3. (20) Write a program that encodes many phrases using the Caesar Cipher. Specifically, your program will ask the user for the name of a file that contains phrases that need to be encoded and a key, read the file of phrases to be encoded, and output the encoded phrase. For example, process the data/phrases.txt file that you copied from the handouts directory.
  4. (30) The updated 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 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 output:

    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

Extra Credit (up to 10 pts)

Implement another game of your choice using the 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.

Finishing up: What to turn in for this lab

  1. IDLE and jEdit may create backup files with the "~" extension. Delete these files from your lab directory to save paper when you print.
  2. Copy your lab7 directory into the turnin directory. (Review the UNIX handout if you don't remember how to do that.)
  3. Before printing, remove the game.pyc file from your labs/lab7 directory. Verify that you have only the .py files you wrote (plus a directory called data).
  4. Use the printLab.sh command to create a file to print out. You should probably print from the labs directory.
  5. 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!

Grading (100 pts)