I am preparing an exam and I have been doing several exercises, the problem arises when I get to the part of object-oriented programming (OOP) in this exercise:
Write a class that allows you to represent the character of a video game. A character has a name (or nickname), a percentage of life (or health), a power (its name, for example, "rotating kick"), and a measure of damage (whole number between 0 and 100).
command Implement a method to print the information of a character using the print ()
Implement a method that allows you to attack another character (which is received as a parameter). The attack of the character (p1) takes life from the character that is attacked (p2), using the following function: new_life (p2) = current_life (p2) - measure_of_damage (p1)
Implement a method that indicates (returning True) if a character is alive (health> 0)
Create 3 characters called pj1, pj2 and pj3 (with the percentage of health and power that you want), pj1 must attack pj2 and pj3 "" "
This is the code I have:
global vida_queda
vida_queda = 0
class Personaje:
def __init__(self, nickname, vida, poder, daño):
self.nickname = nickname
self.vida = vida
self.poder = poder
self.daño = daño
def __repr__(self):
return("su nombre era " + self.nickname + " " + "se decia que habia vivido " + " " + self.vida + " "+ "tantos años" + " " + "su ataque especial era" +" "+ self.poder + " " + "y el daño que infligian era " + " " +self.daño)
def atacar(self,atacante):
vida_queda = int(self.vida) - int(atacante.daño) #Tuve que usar el método int() para que funcionara
return( vida_queda)
def checklife(self):
if vida_queda <= 0: #Tuve que usar el método int() para que funcionara
return("i'm dead")
else:
return(vida_queda)
p1 = Personaje("el_gaucho", str(100), "mono borracho", str(30))
print(p1)
print(p1.nickname)
print(p1.vida)
print(p1.poder)
print(p1.daño)
print("#_________________________________________________________#")
p2 = Personaje("'gaucho_Bataraz'", str(30), "facón giratorio", str(250))
print(p2)
print(p2.nickname)
print(p2.vida)
print(p2.poder)
print(p2.daño)
print("#_________________________________________________________#")
p3 = Personaje("'Dura'", "120", "golpe de campo", "300")
print(p3)
print(p3.nickname)
print(p3.vida)
print(p3.poder)
print(p3.vida)
My problem is that it always returns i'm dead
, when I have clearly tried to attack a character with a lot of life with one with very little damage, is there any
How is the variable saved or is it not?