0

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)

6
  • Works for me. Did you try it again to rule out a mere coincidence? (Also, stylistic notes: Indenting only 4 spaces doesn't hurt (I'd say it's more readable) and you can simlify that cascade of long if statements into total = sum([d1, d2, d3, d4]) - min([d1, d2, d3, d4]) - works even better if you store the rolls in one list from the start) Commented Feb 23, 2011 at 17:53
  • What is the output you get and what is the output you want? Commented Feb 23, 2011 at 17:55
  • 1
    sum(sorted(roll(6) for x in range(4))[-3:]) Commented Feb 23, 2011 at 17:57
  • You did misspell WIZ/WIS. Commented Feb 23, 2011 at 18:02
  • oddly enough, I closed my ide, reopened it, now its working. Must have been some bad formatting or something because I didn't change the code at all. Commented Feb 23, 2011 at 18:06

4 Answers 4

2

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()
Sign up to request clarification or add additional context in comments.

Comments

1

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

Even better: ds = [roll(6) for _ in range(4)] and return sum(ds) - min(ds) ;)
Perhaps - that happens to be python specific shorthand for the algorithm, yes.
It's merely generalized for n dice and thus doesn't requring anyone to type out n lines of nearly identical lines ;) I'd write similar code in every language that allows me to.
After thinking about it, so would I. I guess I can't imagine langauge with out arrays, for loops, and the ability to write the sum and min function if they don't exist.
0

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

0

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

1 Comment

Can you please be a bit clearer? How does this relate to the question?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.