- Practice making and using classes and objects.
For this lab, you will be creating an object-oriented (OO)
program to play a simple dice game. Start with the following:
- Create a PyCharm project. Make sure you use your name somewhere in the
- Create main.py, where you will put the main()
- Create a dice.py module where you will put the Dice
Step I: Write the Dice class
In the dice module, write the Dice class. A Dice
object should have two properties/attributes: the number of sides the
dice has, and the current value showing on the dice. These properties
should be attributes in your class (also known as instance
Add the following methods:
- A constructor that takes a parameter that indicates the number of
sides that the particular dice should have. Be sure that each of
the instance variables is given an appropriate value.
- A roll method that rolls the dice (causes the current value
to be a new random number from 1 to the number of sides). [Note: you can
use randint from the random module;
- A getter method that returns the current value of the dice.
Step II: Write main
In the main module, write the main() function to play
a simple game according to the following rules:
- The game uses a D6 and a D12 (6-sided and 12-sided dice).
- On each turn, we roll both dice.
- The game is won if one of the dice shows a value that's exactly twice
the value of the other.
Your main function should create variables for the two dice:
each should be an object of type Dice.
Your program should use input to ask the user to hit return
before each turn. After each turn, the program should check if we've
won. If we've won, print a message saying so and end the program.
[Extra credit] Now, go back and modify the Dice class so that
the default number of sides is 6, so that if we ask to create a dice
without specifying the number of sides, we will get a D6.
Modify main() so it uses this default as appropriate.
How to turn in this lab Before turning in any program in this
class, remember our mantra: just because it works doesn't mean it's
good. Part of your grade will also come from things like how understandable
and readable your code is. You will also be graded on the neatness,
presentation, and style of your program code.
Compress the project folder and mail the tarball or zip file to me at firstname.lastname@example.org.
Ask for help if you're having problems!
1. I've checked, and "Dice" is an acceptable singular form.
If this bothers you because you consider "Dice" as plural only,
then you should instead create a "Die" class. Classes should be named
with singular nouns.↩