Skip to main content.

Goals

After the lab, you should be proficient at

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

Objective: Review

Review the slides for today's lab.

Objective: Set Up

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. (Note that we are not following our usual filenaming conventions for this problem.)

    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, inclusive
    • rollMultipleDice(numDice, sides) -- simulates rolling multiple dice, each with the same number of sides. Returns the total value from rolling multiple dice.

    Programmatically test each of the functions in a function called testFunctions in the game module. testFunctions will call another test function for each of the other functions (e.g., testFlipCoin, testRollDie). Determining if these functions are correct is a little bit different than what we've done in the past. How can you determine correctness for these functions?

    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 on Friday to a file named lab8.2.py. Modify the program to use the game module every time 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). Display 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 play the game 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 your turnin directory.
  3. Remove the .pyc file before printing, if such a file exists exists.

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

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

Grading (100 pts)