Lab 5: More Practice with Functions

Goals

After the lab, you should be proficient at

  1. solving problems with functions
  2. creating and using your own modules

Linux

As usual, create a directory for the programs and output you develop in this lab. Copy the third program from the last lab (lab4.3.py) into the lab5 directory and rename it lab5.1.py. If you completed that problem, also copy the output (lab4.3.out) and rename it lab5.1.out in this lab.

Objective: Programming in Python

We'll practice writing several Python programs, each in their own text file. Name the files lab5.1.py through lab5.5.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 lab5.x.out, where x is the problem number.

  1. (30) If you have not already done so, finish the Caesar cipher problem from last time.
  2. (15) Several weeks ago, we wrote speedlimit.py, which calculated a fine for speeding. Save the program as lab5.2.py. Then, define a function that takes as input the speed limit and the clocked speed and returns the computed fine. The driver of your program will print an appropriate message based on the returned fine. Then, put the driver part of the program into a main function.
  3. (15) Copy the Caesar Cipher program and modify it so that you now have a function that does the encoding. The function takes as input a string and a key. It returns the encoded string. Modify your program to use this function. Put the driver into a main function.
  4. (15) Modify the previous program to include a function called translateLetter that takes in a letter and key and returns the translated version of the letter. Call the translateLetter function in the function you wrote for the previous program.

    In comments, briefly compare the readablity and efficiency of this program with the previous program.

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

    Constant variables:

    Assign the constants appropriate values.

    Functions:

    Save rainbowdice.py as lab5.5.py and modify it to use the game module and the functions/constants you defined in the module. When you flip the coin, test it against game's variables HEADS and/or TAILS.

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. When you imported the game module in other code, the interpreter created a .pyc file. Delete that file so that your output file isn't huge and doesn't contain strange characters.
  3. Copy your lab5 directory into the turnin directory. (Review the UNIX handout if you don't remember how to do that.)
  4. Turn in your printed lab assignment, using the printLab.sh command. You should probably print from the labs directory.

    Before you print, view the file to make sure it's not too long or has weird characters in it from the .pyc file. One command to view the file is gv lab5.ps

    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 2:25 p.m. on Friday.

Ask well before the deadline if you need help turning in your assignment!

Grading (100 pts)