from random import randrange class MSDie: def __init__(self, sides): if sides > 0: self.sides = sides else: self.sides = 6 self.value = 1 def roll(self): self.value = randrange(1,self.sides+1) def getValue(self): return self.value def setValue(self,value): if 1 <= value <= self.sides: self.value = value else: print("Value {0} is is not from [1,{1}]. Action is aborted.".format(value,self.sides)) def main(): die1 = MSDie(6) # an object of class MSDie with 6 sides: 1 through 6 die1.roll() # rolling the die print("first roll of the die: ",die1.getValue()) die1.setValue(5) # setting the value of the die to 5 print("Setting the value of the die to 5 and checking it:",die1.getValue()) die1.roll() # rolling the die a second time print("second roll of the die: ",die1.getValue()) die2 = MSDie(7) # another object of class MSDie with 7 sides: 1 through 7 die2.roll() # rolling the die print("\n first roll of the second die: ",die2.getValue()) die2.setValue(7) # setting the value of the die to 7 print("Setting the value of the second die to 7 and checking it:",die2.getValue()) die2.roll() # rolling the die a second time print("second roll of the second die: ",die2.getValue()) die2.roll() # rolling the die a second time print("third roll of the second die: ",die2.getValue()) print("\n Trying to set the value of the first die to 8, which is more than 6...") die1.setValue(8) main()