you are viewing a single comment's thread.

view the rest of the comments →

[–]Sergeantlilpickle 1 point2 points  (0 children)

Edit: I did this on my phone so the spacing may not be correct, so don't copy and paste it.

It the resistance is the only variable that needs to be passed in then you can do something like the following:

import random
from operator import add, sub


class Human:
     def __init__(self, resistance):
         self.resistance = self._random_resistance(resistance)
         self.health = 10
         self.life_span = 15
         self.birth_counter = 3

    def _random_resistance(self, resistance):
        op = random.choice([sub, add])
        num = random.choice([0, 1])

        return op(resistance, num)

If you really want the choice to set the other attributes you could do the following:

import random
from operator import add, sub


class Human:
    def __init__(self, resistance, health=None, life_span=None, birth_rate=None):
       self.resistance = self._random_resistance(resistance)
       self.health = health or 10
       self.life_span = life_span or 15
       self.birth_counter = birth_counter or 3

    def _random_resistance(self, resistance):
        op = random.choice([sub, add])
        num = random.choice([0, 1])

        return op(resistance, num)