![](/img/trans.png)
[英]How do i calculate the percentage increase or decrease in a list of values with python
[英]How do I increase a value (num) in a method and decrease another defined in a class in Python?
我正在用python做游戏,我需要创建一种方法来增加/减少我在类中定义的值。 该方法收到一个数字,我必须修改另一个数字。 这是我的代码:
class Ant:
def __init__(self, name, steps, health, alcohol, state):
self.name = name
self.steps = []
self.health = 100
self.alcohol = 0
self.state = state
我需要修改健康状况。请帮助我
使用单个参数定义方法并修改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
您可以创建一些从Ant
的健康中减去的功能。 例如:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.