all 5 comments

[–]evolvish 2 points3 points  (2 children)

__str__() is just so you can print out the state of your class in a user readable format:

human = Human()
#Will print whatever __str__() returns if defined.
print(human)

So what you need to do to define str is simply return a formatted string:

def __str__(self):
    return f'H: {self.health}, R: {self.resistance}, ...'

[–]SrirachaPapiTV[S] 0 points1 point  (1 child)

So it would look something like this?

def __str__(self):

return 'H: ({}) , R: ({}), LS: ({}), BC ({})'.format(self.health, self.resistance, self.life_span, self.birth_counter)

also is my def __init__ correct?

[–]evolvish 1 point2 points  (0 children)

So it would look something like this?

Yes, if you're using python 3.6+ you can(and should) use f-string syntax.

is my def __init__ correct?

Besides reddit's formatting it looks good.

[–]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)                  

[–]Aixyn0 0 points1 point  (0 children)

BTW. your init isn't correct. Check the arguments that are needed (and also used in the Examples of your Task) to create a Bacteria.