Python - Pass a variable to a function -
im curently learning python (2.7) using excellent tutorials http://www.learnpythonthehardway.org/
i trying make small text input game improve skills, part of trying add health meter main character. adding combat reduce health.
the below code designed set players health 100 @ start of each game, executing function "player_health" in class called "set_health"
class health(): def store_health(self): d = set_health() d.player_health() local_health = d.player_health() print "your health at", local_health, "%" return local_health
when below "punch_received" function executed, players health reduced 10
class combat(): def punch_received(self): punch = 10 x = health() x.store_health() combat_health = x.store_health() combat_health = combat_health - punch print "you have been punched, health is", combat_health, "%"
so far good. may not perfect or best methodology works base learn from.
my issue not know how how return/send value of "combat_health" variable e.g. "current_hero_health" in function.
class hero_health(): def current_hero_health(self): # want store running total of heros health in here
any appreciated. deepend
you make current_hero_health
attribute of hero_health
.
class hero_health(): def __init__(self, current_h): self.current_hero_health = current_h def current_hero_health(self): self.current_hero_health = 3 ; # stored total of hero health
you can access member method of class, self.current_hero_health
, , stores global count object of class.
hope helps.
Comments
Post a Comment