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()
You can store the results in a list:
rolls = [Die(n).roll_die() for n in (6, 4, 12)]
then you can show the individual results
>>> print rolls
[5, 2, 6]
or sum them
>>> print sum(rolls)
13
Or, instead, you could keep a running total:
total = 0
for n in (6, 4, 12):
value = Die(n).roll_die()
print "Rolled a", value
total += value
print "Total is", total
(edited to reflect the changes/clarifications to the question)
I'm not sure exactly where you're confused. The simplest thing you need to do is separate the concept of a specific die you're going to roll (the object) with the action (rolling it). I would start here:
d6 = Die() #create die 1 with the default side of 6
d4 = Die(4) #create die 2 with 4 sides
d12 = Die(12) #create die 3 with 12 sides
roll1 = d6.roll_die()
roll2 = d4.roll_die()
roll3 = d12.roll_die()
print "%d\n%d\n%d\nsum = %d" % (roll1, roll2, roll3, roll1 + roll2 + roll3)
... and then get fancier with lists, etc.
It may also be useful to just store the last roll so you can get it whenever you want.
def __init__(self, s = 6):
self.sides = s
self.last_roll = None
def roll_die(self):
self.last_roll = random.randint(1,self.sides)
return self.last_roll
Since roll_die
returns a value, you can add those values.
Try this.
roll1.roll_die() + roll2.roll_die()
What happens?
You can just sum the numbers. In case you want to sum the outcome of n rolls, consider adding this function to the class:
def sum_of_n_rolls(self, n)
return sum(self.roll_die() for _ in range(n))
Also, consider renaming roll_die to just roll . It's obvious that it's not about rolling a rock, since the method is part of the class Die .
Edit : I now read you need to print intermediate rolls. Consider:
def n_rolls(self, n):
return [self.roll_die() for _ in range(n)]
Now you can roll a 7-sided die 10 times:
rolls = Die(7).n_rolls(10)
print(rolls, sum(rolls))
Guess I'd do something like this:
# Create dice
sides = [6,4,12]
dice = [Die(s) for s in sides]
# Roll dice
rolls = [die.roll_die() for die in dice]
# Print rolls
for roll in rolls:
print roll
You can also combine a few of these steps if you like:
for num_sides in [6,4,12]:
print Die(num_sides).roll_die()
If I understood you correctly you want a class attribute.
UPDATE: Added a way for automatically reseting the total
import random
class Die():
_total = 0
@classmethod
def total(cls):
t = cls._total
cls._total = 0
return t
def __init__(self, s=6):
self.sides = s
def roll_die(self):
x = random.randint(1,self.sides)
self.__class__._total += x
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()
print Die.total()
print "--"
print roll1.roll_die()
print roll2.roll_die()
print roll3.roll_die()
print Die.total()
Let's get crazy :) (combined with my last answer as well)
class Die():
def __init__(self, s = 6):
self.sides = s
self.last_roll = None
def roll_die(self):
self.last_roll = random.randint(1,self.sides)
return self.last_roll
dice = map(Die, (6, 4, 12))
rolls = map(Die.roll_die, dice)
print rolls
print sum(rolls)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.