In your glacius
function, damage_done
is a local variable. The damage_done
in global namespace doesn't see the change unless you use the global
keyword in the glacius
function to tell Python damage_done
should be global:
import random
Strength = 5
Magic = 5
Speed = 5
Lifeforce = 5
base_health = Lifeforce * 10 + 50
damage_done = 0
curent_health = base_health - damage_done
##functions for stuff
def glacius():
global damage_done # <---- here
magic_damage = 5 * random.randint(1, 5)
damage_done = magic_damage
def nukapana_moves():
moves = ["Glacius"]
attack = random.choice(moves)
if attack == "Glacius":
glacius()
print(f"nukapana uses {attack} it does {damage_done}.")
nukapana_moves()
note: Often depending on global variables considered a bad practice. Instead you should free your code from depending on a global variable by returning values from functions. Functions should do their jobs independently.
Re-implementation using class:
import random
class Hero:
def __init__(self):
self.strength = 5
self.magic = 5
self.speed = 5
self.lifeforce = 5
self.base_health = self.lifeforce * 10 + 50
self.damage_done = 0
@property
def current_health(self):
return self.base_health - self.damage_done
def glacius(self):
self.damage_done = 5 * random.randint(1, 5)
def nukapana_moves(self, moves):
attack = random.choice(moves)
if attack == "Glacius":
self.glacius()
print(f"nukapana uses {attack} it does {self.damage_done}.")
player = Hero()
print(player.current_health)
player.nukapana_moves(['Glacius'])
print(player.current_health)
output:
100
nukapana uses Glacius it does 25.
75
note: When you hit damage you need to re-calculate the current_health
, Or as I've done here use a property that gives you the correct value. Otherwise you hit damage but the self.current_health
doesn't change because it calculated once in the initializer.