Lab 8: Practice with Modules and Indefinite Loops

Goals

After the lab, you should be proficient at

  1. creating and using your own modules
  2. using indefinite loops
  3. exception handling

Linux

As usual, create a lab8 directory..

Objective: Programming in Python

We'll practice writing several Python programs, each in their own text file. Name the files, as usual.

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 lab8.x.out, where x is the problem number.

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

    Constants:

    Assign the constants appropriate values.

    Functions:

    Test each of the functions in a function called testFunctions in the game module. (That function can call other test functions.) Review the testing examples we did in class if you don't remember what we were doing.

    You can use the __name__ trick we talked about in class so that when you import the module in another script, you don't automatically run testFunctions

  2. (10) Copy consecutiveHeads.py or consecutiveHeads2.py that we wrote in class to a file named lab8.2.py. Modify the script to use the game module everytime it is appropriate. Make sure that you don't duplicate functionality from the game module in your script.
  3. (15) Write a program with a while loop that stops when the user enters a number that is evenly divisible by 6 (that is, there is no remainder). Print helpful output to the user.
  4. (10) Modify your Caesar Cipher program to use exception handling to better handle (i.e., print a more descriptive error message) when a user enters a file name to encode but that file that doesn't exist.
  5. (30) Simulate a simple Craps game (without betting). Break the game into manageable pieces and build up. For example, make your program simulate rolling two dice and then test your program. Then, handle that the person rolls a 2, 3, 7, 11, or 12. Then, handle the point.

    The Rules

    1. A player rolls two dice. (What should you leverage?)
    2. There are three possibilities:
      • 7 or 11 wins. If the total of the dice is 7 or 11 then the player wins.
      • 2, 3, or 12 loses. If the total of the first roll is 2, 3, or 12 then the player loses.
      • Others become the point. If the total is any other number (4, 5, 6, 8, 9, 10) then this number becomes the point.

        The player keeps rolling until one of two things happen. Either the player makes the point and wins, or the player rolls a 7 and loses (craps out). Any number other than the point or 7 is of no consequence.

    After the first roll, print a message that tells the player that they either won, lost, or which "point" they have to play for.

    Example runs:

    *** This program simulates Craps (without the betting) ***
    
    You rolled a 11
    Congratulations!  You win!
    
    *** This program simulates Craps (without the betting) ***
    
    You rolled a 6
    The POINT is 6
    Roll again!
    
    You rolled 4
    Roll again!
    
    You rolled 6
    Congratulations!  You win!
    
    *** This program simulates Craps (without the betting) ***
    
    You rolled a 10
    The POINT is 10
    Roll again!
    
    You rolled 4
    Roll again!
    
    You rolled 9
    Roll again!
    
    You rolled 7
    Sorry!  You crapped out.  Luckily, money wasn't involved.
    

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.

You may want to consider Rainbow Dice, a Sprenkle family tradition. Only run to three points to reduce the amount of output.

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 lab8 directory into the turnin directory. (Review the UNIX handout if you don't remember how to do that.)
  3. Remove the .pyc file before printing
  4. Use the printLab.sh command to create a file to print out. You should probably print from the labs directory.

    View the lab8.ps file using the gv command, e.g., gv lab8.ps. You should not print out the original data files or the generated summary files.

    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)