Lab 7: Practice with Functions

Goals

After the lab, you should be proficient at

  1. refactoring your code to use functions
  2. solving more advanced problems using functions, such as passing a list as a parameter to a function
  3. solving problems using top-down design

Linux

Copy /home/courses/cs111/handouts/lab7 and all of its contents, recursively, into your labs directory. This way, you don't have to create the lab7 directory first. Otherwise, you could first create the lab7 directory and then copy all of the contents of the /home/courses/cs111/handouts/lab7 directory (i.e., add a * to the cp command) into your lab7 directory.

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. (15) Several weeks ago, we wrote speedingticket.py, which calculated a fine for speeding. Copy the program as lab7.1.py so that you have an easy reference to the original solution. Then, define a function that takes as parameters the speed limit and the clocked speed and returns the computed fine. (What should the calculated fine be if the person wasn't speeding?) The code that calls the function will print an appropriate message based on the returned fine. Write an appropriate comment for the function you defined.

    Then, put the driver part of the program (i.e., the part that gets input from the user, calls the function, and displays the output) into a main function.

    Note that this is an example of refactoring, which is a common practice in coding. You write some code, test it, and then reorganize it so that it's better organized and easier to expand/maintain.

  2. (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. Test to make sure the program is still working correctly.

    Then, modify your program to have a function called translateLetter that takes as parameters a letter and a key and returns the translated the letter. Call the translateLetter 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.

  3. (10) Create a list that contains even numbers from 2 to 20, inclusive. Print out the list in two columns that looks like (note the formatting):
      2      12
      4      14
      6      16
      8      18
     10      20
    

    Hint: what are the positions in the list for each displayed element? This is a warm-up for a later problem.

  4. (50) Deal or No Deal.

    dealornodeal.py contains the main function and some additional functions to play the game Deal or No Deal. For this problem, you will fill in the code for the functions to make the game work correctly. You should not modify the main function, and you should not modify the function headers (i.e., don't modify the number of parameters).

    Note that the given program will not execute successfully.

    First, you should read through the program and get an idea of how it works. For example, answer the following questions:

    Tackle the functions in the following order, writing each one and then testing how far your program gets with each one.

    Advice: Don't worry about the formatting of output at first. Do a first pass implementation of all the functions, then go back and refine the functions.

    Testing: You will need to run this program several times to make sure it is working correctly--at least once the whole way through to only two cases left. Test using both of the files in the deal_or_nodeal directory. You can add additional test files.

Extra Credit (up to 10 pts)

Is it possible to make a better offer function? The banker wants to keep the offers low so that the players don't win too much money. However, if the offers are too low, the player will not take the offer and could win more money.

Keep track of how much players are winning/losing in aggregate in a file called payout.dat. We'll consider the payout to be the difference between the player's case and the deal made. If the player does not make a deal, the payout is 0.

Read in the payout from the file. (Start the payout file at 0.) At the end of the game, print the new total payout to the file. Doing this part (reading/writing a file) will earn you 5 extra credit points.

After you've played the game approximately 10 times, you have a pretty good idea of the payouts. (Admittedly, the payout depends on luck and how good your player is.)

Then, try writing a modified offer function and comparing that payout to the other payout. Justify the modifications to the function in comments.

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 deal_or_nodeal).
  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)