# Multi-Side Die¶

## Procedural Example¶

In [11]:
import random
from random import randrange

In [8]:
def ms_die(sides):
value = random.randrange(1, sides+1)
return value

In [7]:
ms_die(6)

Out[7]:
1
In [9]:
def ms_die_multi(sides, num_dice):
values = []
for i in range(num_dice):
values.append(random.randrange(1, sides+1))
return values

In [10]:
ms_die_multi(6, 15)

Out[10]:
[3, 6, 2, 4, 1, 2, 4, 6, 6, 5, 6, 2, 3, 2, 5]

## Class Example¶

In [18]:
class MSDie:
def __init__(self, sides):
self.sides = sides
self.value = 1

def roll(self):
self.value = random.randrange(1, self.sides + 1)

def get_value(self):
return self.value

def set_value(self, value):
self.value = value

In [19]:
die1 = MSDie(6)

In [20]:
type(die1)

Out[20]:
__main__.MSDie
In [21]:
die1.get_value()

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-21-3d921073b941> in <module>()
----> 1 die1.get_value()

<ipython-input-18-4e292e64d134> in get_value(self)
8
9     def get_value(self):
---> 10         return self.value
11
12     def set_value(self, value):

AttributeError: 'MSDie' object has no attribute 'value'
In [22]:
die1.roll()

In [23]:
die1.get_value()

Out[23]:
4
In [28]:
class MSDie2:
def __init__(self, sides):
if sides < 4 or sides > 120:

self.sides = sides
self.roll()

def roll(self):
self.value = random.randrange(1, self.sides + 1)

def get_value(self):
return self.value

def set_value(self, value):
self.value = value

In [41]:
def make_die(sides):
try:
die1 = MSDie2(sides)
print(die1.get_value())
except ValueError:
print("Opps, something happened when creating our die!")

In [42]:
make_die(3)

Opps, something happened when creating our die!

In [43]:
make_die(6)

5


## __repr__¶

In [51]:
class MSDie3:
def __init__(self, sides):
if sides < 4 or sides > 120:

self.sides = sides
self.roll()

def roll(self):
self.value = random.randrange(1, self.sides + 1)

def get_value(self):
return self.value

def set_value(self, value):
self.value = value

def __repr__(self):
s = "MSDie3({0})".format(self.sides)
return s

def __str__(self):
s = "MSDie3<{0}>: {1}".format(self.sides, self.value)
return s

In [52]:
def make_dice(qty, sides):
bag_o_dice = []

for d in range(qty):
bag_o_dice.append(MSDie3(sides))

return bag_o_dice

In [54]:
dice = make_dice(100, 6)

In [55]:
print(dice)

[MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6), MSDie3(6)]

In [56]:
# When printed, one die object calls its __str__() method
print(dice[43])

MSDie3<6>: 3

In [58]:
# The same die object isn't printed, but evaluated and __repr__ prints
# It's unabigious representation of the object.
dice[43]

Out[58]:
MSDie3(6)
In [59]:
dice[43].get_value()

Out[59]:
3
In [63]:
for die in dice:
print("Before roll()", die)
die.roll()
print("After roll()", die)
print()

Before roll() MSDie3<6>: 2
After roll() MSDie3<6>: 3

Before roll() MSDie3<6>: 6
After roll() MSDie3<6>: 3

Before roll() MSDie3<6>: 1
After roll() MSDie3<6>: 1

Before roll() MSDie3<6>: 2
After roll() MSDie3<6>: 1

Before roll() MSDie3<6>: 3
After roll() MSDie3<6>: 4

Before roll() MSDie3<6>: 1
After roll() MSDie3<6>: 3

Before roll() MSDie3<6>: 4
After roll() MSDie3<6>: 5

Before roll() MSDie3<6>: 4
After roll() MSDie3<6>: 1

Before roll() MSDie3<6>: 4
After roll() MSDie3<6>: 1

Before roll() MSDie3<6>: 4
After roll() MSDie3<6>: 4

Before roll() MSDie3<6>: 1
After roll() MSDie3<6>: 2

Before roll() MSDie3<6>: 4
After roll() MSDie3<6>: 3

Before roll() MSDie3<6>: 5
After roll() MSDie3<6>: 1

Before roll() MSDie3<6>: 4
After roll() MSDie3<6>: 3

Before roll() MSDie3<6>: 1
After roll() MSDie3<6>: 1

Before roll() MSDie3<6>: 2
After roll() MSDie3<6>: 2

Before roll() MSDie3<6>: 3
After roll() MSDie3<6>: 5

Before roll() MSDie3<6>: 3
After roll() MSDie3<6>: 2

Before roll() MSDie3<6>: 4
After roll() MSDie3<6>: 3

Before roll() MSDie3<6>: 6
After roll() MSDie3<6>: 2

Before roll() MSDie3<6>: 2
After roll() MSDie3<6>: 5

Before roll() MSDie3<6>: 5
After roll() MSDie3<6>: 6

Before roll() MSDie3<6>: 2
After roll() MSDie3<6>: 3

Before roll() MSDie3<6>: 1
After roll() MSDie3<6>: 4

Before roll() MSDie3<6>: 1
After roll() MSDie3<6>: 2

Before roll() MSDie3<6>: 4
After roll() MSDie3<6>: 3

Before roll() MSDie3<6>: 2
After roll() MSDie3<6>: 2

Before roll() MSDie3<6>: 4
After roll() MSDie3<6>: 2

Before roll() MSDie3<6>: 6
After roll() MSDie3<6>: 3

Before roll() MSDie3<6>: 1
After roll() MSDie3<6>: 3

Before roll() MSDie3<6>: 1
After roll() MSDie3<6>: 2

Before roll() MSDie3<6>: 3
After roll() MSDie3<6>: 6

Before roll() MSDie3<6>: 5
After roll() MSDie3<6>: 3

Before roll() MSDie3<6>: 3
After roll() MSDie3<6>: 2

Before roll() MSDie3<6>: 6
After roll() MSDie3<6>: 6

Before roll() MSDie3<6>: 1
After roll() MSDie3<6>: 1

Before roll() MSDie3<6>: 5
After roll() MSDie3<6>: 4

Before roll() MSDie3<6>: 5
After roll() MSDie3<6>: 2

Before roll() MSDie3<6>: 2
After roll() MSDie3<6>: 3

Before roll() MSDie3<6>: 2
After roll() MSDie3<6>: 2

Before roll() MSDie3<6>: 4
After roll() MSDie3<6>: 1

Before roll() MSDie3<6>: 1
After roll() MSDie3<6>: 2

Before roll() MSDie3<6>: 5
After roll() MSDie3<6>: 2

Before roll() MSDie3<6>: 6
After roll() MSDie3<6>: 4

Before roll() MSDie3<6>: 2
After roll() MSDie3<6>: 5

Before roll() MSDie3<6>: 5
After roll() MSDie3<6>: 5

Before roll() MSDie3<6>: 6
After roll() MSDie3<6>: 4

Before roll() MSDie3<6>: 1
After roll() MSDie3<6>: 6

Before roll() MSDie3<6>: 4
After roll() MSDie3<6>: 2

Before roll() MSDie3<6>: 6
After roll() MSDie3<6>: 2

Before roll() MSDie3<6>: 6
After roll() MSDie3<6>: 1

Before roll() MSDie3<6>: 6
After roll() MSDie3<6>: 1

Before roll() MSDie3<6>: 4
After roll() MSDie3<6>: 3

Before roll() MSDie3<6>: 4
After roll() MSDie3<6>: 4

Before roll() MSDie3<6>: 6
After roll() MSDie3<6>: 3

Before roll() MSDie3<6>: 2
After roll() MSDie3<6>: 1

Before roll() MSDie3<6>: 2
After roll() MSDie3<6>: 1

Before roll() MSDie3<6>: 4
After roll() MSDie3<6>: 3

Before roll() MSDie3<6>: 1
After roll() MSDie3<6>: 5

Before roll() MSDie3<6>: 1
After roll() MSDie3<6>: 2

Before roll() MSDie3<6>: 5
After roll() MSDie3<6>: 2

Before roll() MSDie3<6>: 4
After roll() MSDie3<6>: 4

Before roll() MSDie3<6>: 5
After roll() MSDie3<6>: 6

Before roll() MSDie3<6>: 6
After roll() MSDie3<6>: 2

Before roll() MSDie3<6>: 3
After roll() MSDie3<6>: 6

Before roll() MSDie3<6>: 2
After roll() MSDie3<6>: 5

Before roll() MSDie3<6>: 3
After roll() MSDie3<6>: 2

Before roll() MSDie3<6>: 3
After roll() MSDie3<6>: 2

Before roll() MSDie3<6>: 6
After roll() MSDie3<6>: 2

Before roll() MSDie3<6>: 4
After roll() MSDie3<6>: 5

Before roll() MSDie3<6>: 5
After roll() MSDie3<6>: 2

Before roll() MSDie3<6>: 1
After roll() MSDie3<6>: 2

Before roll() MSDie3<6>: 3
After roll() MSDie3<6>: 5

Before roll() MSDie3<6>: 1
After roll() MSDie3<6>: 6

Before roll() MSDie3<6>: 5
After roll() MSDie3<6>: 2

Before roll() MSDie3<6>: 1
After roll() MSDie3<6>: 4

Before roll() MSDie3<6>: 1
After roll() MSDie3<6>: 4

Before roll() MSDie3<6>: 4
After roll() MSDie3<6>: 5

Before roll() MSDie3<6>: 2
After roll() MSDie3<6>: 1

Before roll() MSDie3<6>: 5
After roll() MSDie3<6>: 3

Before roll() MSDie3<6>: 5
After roll() MSDie3<6>: 1

Before roll() MSDie3<6>: 4
After roll() MSDie3<6>: 2

Before roll() MSDie3<6>: 6
After roll() MSDie3<6>: 5

Before roll() MSDie3<6>: 5
After roll() MSDie3<6>: 2

Before roll() MSDie3<6>: 3
After roll() MSDie3<6>: 3

Before roll() MSDie3<6>: 4
After roll() MSDie3<6>: 3

Before roll() MSDie3<6>: 5
After roll() MSDie3<6>: 3

Before roll() MSDie3<6>: 5
After roll() MSDie3<6>: 6

Before roll() MSDie3<6>: 6
After roll() MSDie3<6>: 2

Before roll() MSDie3<6>: 3
After roll() MSDie3<6>: 6

Before roll() MSDie3<6>: 5
After roll() MSDie3<6>: 5

Before roll() MSDie3<6>: 2
After roll() MSDie3<6>: 6

Before roll() MSDie3<6>: 4
After roll() MSDie3<6>: 6

Before roll() MSDie3<6>: 4
After roll() MSDie3<6>: 6

Before roll() MSDie3<6>: 5
After roll() MSDie3<6>: 5

Before roll() MSDie3<6>: 2
After roll() MSDie3<6>: 3

Before roll() MSDie3<6>: 5
After roll() MSDie3<6>: 2

Before roll() MSDie3<6>: 1
After roll() MSDie3<6>: 5

Before roll() MSDie3<6>: 4
After roll() MSDie3<6>: 6

Before roll() MSDie3<6>: 3
After roll() MSDie3<6>: 5


In [66]:
print(dice[0])

MSDie3<6>: 3

In [ ]: