Skip to main content.

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. writing automated test functions
  3. using indefinite loops

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. (20) Copy your lab7.4.py as caesarcipher.py into this (lab8) directory.

    Use the __name__ trick we talked about in class so that when you import the module in another script, you don't automatically run main

    In your lab8.1.py file, import everything from caesarcipher.py. Then, write two test functions, one that tests encodePhrase and one that tests encodeLetter. Call each of the test functions and demonstrate that the tests and original code work.

  2. (25) 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. Make sides have a default value of 6.
    • rollMultipleDice(numDice, sides) -- simulates rolling multiple dice, each with the same number of sides. Returns the total value from rolling multiple dice.

    Again, use the __name__ trick.

    Since it's more difficult to programmatically test a non-deterministic function using a test function, you don't need to write test functions for this module. However, you can if you want.

    If you don't test programmatically, make sure that you thoroughly test the function (e.g., call each function many times within one run and verify that the results are accurate).

    If you choose to programmatically test each of the functions, create a function called testFunctions in the game module. testFunctions will call a 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?

  3. (10) Copy consecutiveHeads.py or consecutiveHeads2.py that we wrote in class on Monday to a file named lab8.3.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.
  4. (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.
  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.
  4. Perform the following steps from your cs111 directory.
    Note that each command below links to a page with more information about using the command.

  5. Create the printable lab assignment, using the createPrintableLab command:
    createPrintableLab <labdirname>
  6. View your file using the evince command.
  7. Print the file using the lpr command.
  8. Log out of your machine when you are done.

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 Friday.

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

Grading (100 pts)