Cover page images (keyboard)

Lab Exercise: Classes



Press space bar for next slide

Our task

We've seen how to create and instantiate our own classes in the lecture. We're going to design a simple class in this lab to roll a die.

Some simple specifications for this class:

These constraints should be enough to design a simple yet effective die class. At the end, we'll make a program that will roll any number of dice.










Step 0












Step 1

The die.py file

Since we need to have our Die class in a module called "die", it needs to be in a file called "die.py". Create that file now in your lab directory and create a simple class with all the methods specified, but do not fill the methods in yet; simply put "pass" as their contents if they don't return anything, as in this sample class/method definition:

class Foo:
    def __init__(self):
        pass

Fill out your sample classes now. Remember that you should have an __init__() method (the constructor) as well as a method for rolling and a method for getting the number of sides.

So we can be consistent with the interface, have both of the non-constructor methods return zero.










Step 2

The empty Die class

Your class should look similar to this now:

class Die:
    def __init__(self, sides):
        # We don't do anything right now in the constructor,
        # but we will later.
        pass
    
    def roll(self):
        # We're not rolling yet, but anything trying to use us will
        # expect a return value.
        return 0
        
    def getSides(self):
        # We don't track the number of sides yet, but anything
        # to use us will expect a return value.
        return 0









Step 3

Testing the shell out

That's not a bad start. It's enough to begin testing it out! Run the Python interpreter in the same directory as the file and give it a little test, like so:

>>> import die
>>> d10 = die.Die(10)
>>> d10
<die.Die instance at 0x6b3f0>
>>> d10.roll()
0
>>> d10.getSides()
0

Your output should look like the above (don't worry if the address of the object is different; it will be different on every computer). Note that though we are creating the die with a specified number of sides in the constructor, we are not yet actually keeping track of how many sides we have, so Die.getSides() still returns zero.

Let's move on to some more implementation.










Step 4

Implementing the constructor

We're fortunate in that we have the constructor pretty much mapped out already; we know what parameters it needs to take, but we're just not storing the number of sides on the die yet. Take a minute to fill that out in the constructor, and while you're at it, fill out the Die.getSides() method (it should be pretty simple). Once finished, move on to the next slide.










Step 5

The filled-out constructor and getSides methods

Your class should look similar to this at this point:

class Die:
    def __init__(self, sides):
        # Just copy over the sides value.                               
        self.sides = sides

    def roll(self):
        # We're not rolling yet, but anything trying to use us will       
        # expect a return value.                                             
        return 0

    def getSides(self):
        # Just return the number of sides.         
        return self.sides

We're almost done! Now we just need to implement the roller.
It's quite simple, as you'll soon see; routines in Python's built-in random module will do most of what we need.










Step 6

Implementing the Die.roll() method

What we want from a die roll is an integer from 1 to the number of sides, inclusive. This is quite easy to implement with the randint() function in the Python random library.

The documentation for the random.randint() function says the following:

random.randint(a, b)
Return a random integer N such that a <= N <= b.

Thus, random.randint() returns an integer in the range [a, b]. Using that information, quickly fill out the Die.roll() method; it should be fairly simple. We'll show the completed class on the next slide in a minute or two.










Step 7

The completed class

Your die.py file should look something like the following (note the addition of the "import random" line at the top):

#!/usr/bin/env python                                                           

import random

class Die:
    def __init__(self, sides):
        # Just copy over the sides value.                                       
        self.sides = sides

    def roll(self):
        # Return a random integer in the proper range.                          
        return random.randint(1, self.sides)

    def getSides(self):
        # Just return the number of sides.                                      
        return self.sides

Next, we'll test the class out.










Step 8

Testing the completed class

Put your class through its paces. Here's an example:

>>> import die
>>> d10 = die.Die(10)
>>> d10
<die.Die instance at 0x6b3f0>
>>> d10.getSides()
10
>>> d10.roll()
8
>>> d10.roll()
10
>>> d10.roll()
5
>>> d10.roll()
8
>>> d10.roll()
1









Bonus Step

If your class worked fine and you're looking for some extra credit, try making a Python dice-roller script that takes user input for the number of dice to roll and how many sides the dice should have, then returns the total of the rolls. We won't give the answer, but show your results to the TA.