After the lab, you should be proficient at
As usual, create a lab8
directory..
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.
game.py
that contains some useful game variables and functions.
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, inclusiverollMultipleDice(numDice, sides)
-- simulates
rolling multiple dice, each with the same number of sides.
Returns the total value from rolling multiple dice.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
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.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.The Rules
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.
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.
turnin
directory. (Review
the UNIX handout if you don't remember how
to do that.)
.pyc
file before
printingprintLab.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!