I'm working on a problem which uses a python class and has a constructor function to give the number of sides to one die and a function to roll the die with a random number returned based on the number of sides. I realize the code is very basic, but I'm having troubles understanding how to sum up the total of three rolled dice with different sides. Since a variable is passing the function instance what would be the best way to grab that value to add it up? Here is what I have.
*To clarify... I can get the totals of the roll1.roll_dice() to add up, but I have to show each roll individually and then the total of the three dice. I can do either one of those but not both.
class Die():
def __init__(self, s = 6):
self.sides = s
def roll_die(self):
x = random.randint(1,self.sides)
return x
roll1 = Die() #Rolling die 1 with the default side of 6
roll2 = Die(4) #Rolling die 2 with 4 sides
roll3 = Die(12) #Rolling die 3 with 12 sides
print roll1.roll_die()
print roll2.roll_die()
print roll3.roll_die()