I am trying to learn Python OOP and have been struggling to understand scope, passing values and how to encapsulate. My earlier attempts have rapidly become spaghetti code (likely because my only programming experience was on 8 bit BASIC 40 years ago), and thus, I am trying classes and objects. Here it is:
import random
class Player:
def __init__(self, weapon, health):
self.weapon = weapon
self.health = health
def reduce_health(self, amount):
self.health -= amount
def check_dead(self):
if self.health > 0:
return "Player not dead"
else:
return "Player dead"
def calculate_damage(self, weapon):
damage_inflicted = random.randint(3, 15) weapon
return damage_inflicted
def fight(self):
player.reduce_health(self.calculate_damage(self.weapon))
print(player.health)
player.check_dead()
player = Player(1, 15)
player.fight()
The calculate_damage function is flagged as being 'static' and the check_dead function seemingly has no effect at all. I need some hints to get my footing. Thanks.
CodePudding user response:
There are a few errors in your code.
First, you are using the player variable in the fight function. This works, because you've called the instance of the Player
class player
, but it wouldn't if you would give it any other name. To fix your issue, use self
instead ofplayer
in all class functions. The self
argument is automatically passed in by python and does always refer to the object on which the the function is called.
Second, the check_dead
function does nothing because it only returns a value. There is no attribute set or value printed out in this function. In order to make it work, you need to print the value, either inside the function or outside, with the return value of the function.
This code should work:
import random
class Player:
def __init__(self, weapon, health):
self.weapon = weapon
self.health = health
def reduce_health(self, amount):
self.health -= amount
def check_dead(self):
if self.health > 0:
return "Player not dead"
else:
return "Player dead"
def calculate_damage(self, weapon):
damage_inflicted = random.randint(3, 15) weapon
return damage_inflicted
def fight(self):
self.reduce_health(self.calculate_damage(self.weapon))
print(player.health)
print(self.check_dead())
player = Player(1, 15)
player.fight()
In this code, we print the aliveness of the player directly instead of returning it:
import random
class Player:
def __init__(self, weapon, health):
self.weapon = weapon
self.health = health
def reduce_health(self, amount):
self.health -= amount
def check_dead(self):
if self.health > 0:
print("Player not dead")
else:
print("Player dead")
def calculate_damage(self, weapon):
damage_inflicted = random.randint(3, 15) weapon
return damage_inflicted
def fight(self):
self.reduce_health(self.calculate_damage(self.weapon))
print(player.health)
self.check_dead()
player = Player(1, 15)
player.fight()
CodePudding user response:
The problem that you are having is probably due to the way you refer to the functions of check_dead(), reduce_health() and the variable health in fight(). You are calling out the player class instance, instead of the instance the function is running in, that meant that, if you were to change the name of the player class instance to something different, it would spill out an error that "player is not defined".
Why the check_dead() is not working could also be because it doesn't print out the values, it just returns them, and it you don't do anything with them when it returns them to the calling function it just scraps them.
I recommend checking the code if it is spelled right and also replace every reference in the class to self. And also make the check_dead() function wrapped in print in the fight() function. That would end up looking something like this:
import random
class Player:
def __init__(self, weapon, health):
self.weapon = weapon
self.health = health
def reduce_health(self, amount):
self.health -= amount
def check_dead(self):
if self.health > 0:
return "Player not dead"
else:
return "Player dead"
def calculate_damage(self, weapon):
damage_inflicted = random.randint(3, 15) weapon
return damage_inflicted
def fight(self):
self.reduce_health(self.calculate_damage(self.weapon))
print(self.health)
print(self.check_dead())
player = Player(1, 15)
player.fight()
If this still doesn't work it's propablly of your running environment or your ide/editor.
Also:
Static means, that the function standalone
If a function is static it doesn't use any of the functions/variables the class gives it (which are commonly gotten by using self.[function/variable name]). By that it is independent and it could be written outside of the class and it would still work as intended.