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)
4 Answers 4
I too get random values on each run.
As for style, you can shrink that code considerably:
class lifeform:
def __init__(self, name):
self.name = name
self.attributes = { 'STR': 0, 'DEX': 0, 'CON': 0, 'INT': 0, 'WIZ': 0, 'CHA': 0, }
def rollAttribute(self):
# roll four 6sided di
dice = [roll(6) for i in range(4)]
# discard lowest roll
return sum(dice) - min(dice)
def rollStats(self):
for key in self.attributes:
self.attributes[key] = self.rollAttribute()
Comments
This doesn't answer your question, it's just a side note. I'd put it in a comment but I can't for formatting.
I always found the dnd die rolling to be more elegant when you did this:
d1 = roll(6)
d2 = roll(6)
d3 = roll(6)
d4 = roll(6)
min = d1;
if d2 < min: min = d2
if d3 < min: min = d3
if d4 < min: min = d4
return d1 + d2 + d3 + d4 - min
4 Comments
sum and min function if they don't exist.It should, and when I run your code, it does.
> python temp.py
{'DEX': 0, 'CHA': 0, 'INT': 0, 'WIS': 0, 'STR': 0, 'CON': 0}
{'DEX': 17, 'CHA': 7, 'INT': 15, 'WIS': 12, 'STR': 15, 'CON': 7}
Although note you have the abbreviation for Wisdom misspelled in the initializer (WIZ not WIS), so at first that was not being updated. Aside from that, it works fine for me.
Comments
Python is a programming language in a matrix of non-negative integers less than the percentage of the element adjacent route will start from the first element to the last element is the least element ends Vmjmv
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.