I am making a game in python and I need to create a method to increase/decrease a value that I define in a class. The method receives a number and with that i have to modify the other. This is my code:
class Ant:
def __init__(self, name, steps, health, alcohol, state):
self.name = name
self.steps = []
self.health = 100
self.alcohol = 0
self.state = state
I need to modify the health.. Help me please
Define a method with a single argument and modify the self.health
:
class Ant:
def __init__(self, name, steps, health, alcohol, state):
self.name = name
self.steps = []
self.health = 100
self.alcohol = 0
self.state = state
def add_health(self, value):
self.health += value
# bonus
def drink(self, value):
self.alcohol += value
self.health -= value
You can make some function that subtracts from the Ant
s health. For example:
class Ant:
def __init__(self, name, steps, health, alcohol, state):
self.name = name
self.steps = []
self.health = 100
self.alcohol = 0
self.state = state
def damage(self, amount):
self.health -= amount
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.