The following code doesn't perform how I expected it to and I can't figure out why. I'm relatively new to python and very confused. both times I display x.attributes they're all set to 0. shouldn't rollStats() be updating them?
import random
def roll(size):
return random.randint(1, size)
class lifeform:
def __init__(self, name):
self.name = name
self.attributes = { 'STR': 0, 'DEX': 0, 'CON': 0, 'INT': 0, 'WIS': 0, 'CHA': 0, }
def rollAttribute(self):
# roll four 6sided di
d1 = roll(6)
d2 = roll(6)
d3 = roll(6)
d4 = roll(6)
# discard lowest roll
if d1 < d2 and d1 < d3 and d1 < d4: total = d2 + d3 + d4
elif d2 < d1 and d2 < d3 and d2 < d4: total = d1 + d3 + d4
elif d3 < d1 and d3 < d2 and d3 < d4: total = d1 + d2 + d4
else: total = d1 + d2 + d3
return total
def rollStats(self):
self.attributes['STR'] = self.rollAttribute()
self.attributes['DEX'] = self.rollAttribute()
self.attributes['CON'] = self.rollAttribute()
self.attributes['INT'] = self.rollAttribute()
self.attributes['WIS'] = self.rollAttribute()
self.attributes['CHA'] = self.rollAttribute()
x = lifeform("test")
print x.attributes
x.rollStats()
print x.attributes
EDIT: here's the output I get btw
$ python fight.py
{'DEX': 0, 'CHA': 0, 'INT': 0, 'WIS': 0, 'STR': 0, 'CON': 0}
{'DEX': 0, 'CHA': 0, 'INT': 0, 'WIS': 0, 'STR': 0, 'CON': 0}
(I originally had a typo in code spelling "WIS" as "WIZ", I corrected that but the problem still exists)
ifstatements intototal = sum([d1, d2, d3, d4]) - min([d1, d2, d3, d4])- works even better if you store the rolls in one list from the start)sum(sorted(roll(6) for x in range(4))[-3:])WIZ/WIS.