Lab 8: Practice with Modules and Indefinite Loops
Goals
After the lab, you should be proficient at
- creating and using your own modules
- writing automated test functions
- using indefinite loops
Objective: Review
Review the slides for today's lab.
Objective: Set Up
- Set up the help client:
run runHelpClient &
- 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.
- (20) Copy your
lab7.4.py
ascaesarcipher.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 runmain
In your
lab8.1.py
file, import everything fromcaesarcipher.py
. Then, write two test functions, one that testsencodePhrase
and one that testsencodeLetter
. Call each of the test functions and demonstrate that the tests and original code work. - (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 eitherHEADS
orTAILS
rollDie(sides)
-- returns a random value between 1 and sides, inclusive. Makesides
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 thegame
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? - (10) Copy
consecutiveHeads.py
orconsecutiveHeads2.py
that we wrote in class on Monday to a file namedlab8.3.py
. Modify the program to use thegame
module every time it is appropriate. Make sure that you don't duplicate functionality from the game module in your script. - (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. - (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
- A player rolls two dice. (What should you leverage?)
- 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
- IDLE and jEdit may create backup files with the "~" extension. Delete these files from your lab directory to save paper when you print.
- Copy
your
lab8
directory into yourturnin
directory. - Remove the
.pyc
file before printing, if such a file exists exists. - Create the printable lab assignment, using the
createPrintableLab
command:
createPrintableLab <labdirname> - View your file using the
evince
command. - Print the file using
the
lpr
command. - Log out of your machine when you are done.
Perform the following steps from
your cs111
directory.
Note that
each command below links to a page with more information about
using the command.
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)
- Python programs: 100 pts; see above for breakdown