Lists of Objects

In [22]:
import random

class MSDie2:
    def __init__(self, sides):
        if sides < 4 or sides > 120:
            raise ValueError("Bad side number!")
            
        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):
        return "<MSDie2({0}): {1}>".format(self.sides, self.value)
In [23]:
single_die = MSDie2(6)
In [24]:
single_die
Out[24]:
<MSDie2(6): 1>
In [25]:
single_die.get_value()
Out[25]:
1
In [26]:
bunch_dice = []
In [27]:
bunch_dice.append(single_die)
In [28]:
bunch_dice
Out[28]:
[<MSDie2(6): 1>]
In [29]:
single_die2 = MSDie2(8)
In [30]:
bunch_dice.append(single_die2)
In [32]:
bunch_dice
Out[32]:
[<MSDie2(6): 1>, <MSDie2(8): 4>]
In [33]:
len(bunch_dice)
Out[33]:
2
In [34]:
def diehard(n):
    bag_of_dice = []
    
    for i in range(n):
        a_die = MSDie2(12)
        bag_of_dice.append(a_die)
        
    return bag_of_dice
In [35]:
my_dice_bag = diehard(10)
print(my_dice_bag)
[<MSDie2(12): 8>, <MSDie2(12): 11>, <MSDie2(12): 12>, <MSDie2(12): 2>, <MSDie2(12): 9>, <MSDie2(12): 1>, <MSDie2(12): 3>, <MSDie2(12): 10>, <MSDie2(12): 2>, <MSDie2(12): 9>]
In [43]:
def rolldice(dice_list):
    for die in dice_list:
        # Temporarily, let's say the first MSDie2 object in this list is known by two names:
        # die and dice_list[0]
        #print(die)
        die.roll()
        
def rolldice_alt(dice_list):
    for i in range(len(dice_list)):
        dice_list[i].roll()
In [44]:
print(my_dice_bag)
print()
rolldice(my_dice_bag)
print()
print(my_dice_bag)
[<MSDie2(12): 1>, <MSDie2(12): 7>, <MSDie2(12): 10>, <MSDie2(12): 12>, <MSDie2(12): 7>, <MSDie2(12): 6>, <MSDie2(12): 2>, <MSDie2(12): 8>, <MSDie2(12): 1>, <MSDie2(12): 5>]


[<MSDie2(12): 2>, <MSDie2(12): 1>, <MSDie2(12): 4>, <MSDie2(12): 5>, <MSDie2(12): 2>, <MSDie2(12): 5>, <MSDie2(12): 7>, <MSDie2(12): 8>, <MSDie2(12): 5>, <MSDie2(12): 5>]
In [45]:
print(my_dice_bag)
print()
rolldice_alt(my_dice_bag)
print()
print(my_dice_bag)
[<MSDie2(12): 2>, <MSDie2(12): 1>, <MSDie2(12): 4>, <MSDie2(12): 5>, <MSDie2(12): 2>, <MSDie2(12): 5>, <MSDie2(12): 7>, <MSDie2(12): 8>, <MSDie2(12): 5>, <MSDie2(12): 5>]


[<MSDie2(12): 4>, <MSDie2(12): 1>, <MSDie2(12): 8>, <MSDie2(12): 6>, <MSDie2(12): 9>, <MSDie2(12): 10>, <MSDie2(12): 8>, <MSDie2(12): 6>, <MSDie2(12): 2>, <MSDie2(12): 10>]

Tuples

In [46]:
type(my_dice_bag)
Out[46]:
list
In [48]:
my_dice_bag_t = tuple(my_dice_bag)
In [49]:
type(my_dice_bag_t)
Out[49]:
tuple
In [50]:
print(my_dice_bag_t)
(<MSDie2(12): 4>, <MSDie2(12): 1>, <MSDie2(12): 8>, <MSDie2(12): 6>, <MSDie2(12): 9>, <MSDie2(12): 10>, <MSDie2(12): 8>, <MSDie2(12): 6>, <MSDie2(12): 2>, <MSDie2(12): 10>)
In [54]:
my_dice_bag[5] = None
In [55]:
my_dice_bag_t[5] = None
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-55-4372e39f7e3c> in <module>()
----> 1 my_dice_bag_t[5] = None

TypeError: 'tuple' object does not support item assignment
In [56]:
my_dice_bag
Out[56]:
[<MSDie2(12): 4>,
 <MSDie2(12): 1>,
 <MSDie2(12): 8>,
 <MSDie2(12): 6>,
 <MSDie2(12): 9>,
 None,
 <MSDie2(12): 8>,
 <MSDie2(12): 6>,
 <MSDie2(12): 2>,
 <MSDie2(12): 10>]
In [57]:
del my_dice_bag[5]
In [58]:
my_dice_bag
Out[58]:
[<MSDie2(12): 4>,
 <MSDie2(12): 1>,
 <MSDie2(12): 8>,
 <MSDie2(12): 6>,
 <MSDie2(12): 9>,
 <MSDie2(12): 8>,
 <MSDie2(12): 6>,
 <MSDie2(12): 2>,
 <MSDie2(12): 10>]
In [ ]: