A error with objects in python | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 1

A error with objects in python

Hi, I am doing a code in python for a text game, i am learning python, i made this code: class Personaje: def __init__(self): self.vida = 1900 self.max_vida = self.vida def hit(self,daƱo): self.vida -= self.daƱo if self.vida <= 0: self.vida = 0 def heal(self,healing): if self.vida >= 0: self.vida += self.healing if self.vida > self.max_vida: self.vida = self.max_vida def estado(self): if self.vida > 0: return "Viva" + "\n" + self.vida + "\n" elif self.vida <= 0: self.vida = 0 return "Muerta" + "\n" + self.vida + "\n" Maeve = Personaje print (Maeve.estado()) Maeve.hit(200) print(Maeve.estado()) Maeve.heal(500) print(Maeve.estado()) Maeve.hit(1899) print(Maeve.estado()) Maeve.heal(700) print(Maeve.estado()) Maeve.hit(2000) print(Maeve.estado()) but this code give me a error: ====== RESTART: D:/Users/Usuario/Documents/Python projects/Personaje.py ====== Traceback (most recent call last): File "D:/Users/Usuario/Documents/Python projects/Personaje.py", line 23, in <module> print (Maeve.estado()) TypeError: estado() missing 1 required positional argument: 'self' >>> and I can't understand this error, pls help me to fix it PD: I am learning English, sorry for my English

31st Oct 2017, 2:30 AM
Emmanuel David Angarita Quintanilla
Emmanuel David Angarita Quintanilla - avatar
7 Respostas
+ 1
You defined your methods like def methodName(self,x) That is redundant. The self is available always. Objects know themselves. Write def methodName(x) Without the self. That should do it.
31st Oct 2017, 2:39 AM
1of3
1of3 - avatar
0
I just solucioned, if i use: class Personaje: def __init__(self): self.vida = 1900 self.max_vida = self.vida def hit(daƱo): self.vida -= self.daƱo if self.vida <= 0: self.vida = 0 Python gives me this error: Traceback (most recent call last): File "D:/Users/Usuario/Documents/Python projects/Personaje.py", line 26, in <module> Maeve.hit(200) TypeError: hit() takes 1 positional argument but 2 were given but if i declare the variables use in the arguments in the __init__: class Personaje: def __init__(self): self.vida = 1900 self.max_vida = self.vida self.daƱo = 0 def hit(self,daƱo): self.vida -= self.daƱo if self.vida <= 0: self.vida = 0 Python no gives me a error, the correct program is this: class Personaje: def __init__(self): self.vida = 1900 self.max_vida = self.vida self.daƱo = 0 self.healing = 0 def hit(self,daƱo): self.vida -= self.daƱo if self.vida <= 0: self.vida = 0 def heal(self,healing): if self.vida >= 0: self.vida += self.healing if self.vida > self.max_vida: self.vida = self.max_vida def estado(self): if self.vida > 0: return "Viva" + "\n" + str(self.vida) elif self.vida <= 0: self.vida = 0 return "Muerta" + "\n" + str(self.vida) Maeve = Personaje() print (Maeve.estado()) Maeve.hit(200) print(Maeve.estado()) Maeve.heal(500) print(Maeve.estado()) Maeve.hit(1899) print(Maeve.estado()) Maeve.heal(700) print(Maeve.estado()) Maeve.hit(2000) print(Maeve.estado()) but i have a new error, the program exit is: Viva 1900 Viva 1900 Viva 1900 Viva 1900 Viva 1900 Viva 1900 when i expected: Viva 1900 Viva 1700 Viva 1900 Viva 1 Viva 701 Muerta 0
31st Oct 2017, 3:03 AM
Emmanuel David Angarita Quintanilla
Emmanuel David Angarita Quintanilla - avatar
31st Oct 2017, 4:02 AM
Emmanuel David Angarita Quintanilla
Emmanuel David Angarita Quintanilla - avatar
0
can you provide a text copy in the solo learn compiler ? it's too hard to read it in here. and you can't put a method inside a print() functions NO : print (Maeve.estado())
31st Oct 2017, 4:26 AM
jay
0
self.vida -= self.daƱo #this line is wrong you're making a new instance variable that is not defined you can do this instead self.vida -= dano same in here , you can't do this ( self.vida += self.healing) do this instead. self.vida +=healing
31st Oct 2017, 4:39 AM
jay
0
THIS WILL WORK . TRY IT class Personaje(): def __init__(self): self.vida = 1900 self.max_vida = self.vida def hit(self,daƱo): self.vida -=daƱo if self.vida <= 0: self.vida = 0 def heal(self,healing): if self.vida >= 0: self.vida +=healing if self.vida > self.max_vida: self.vida = self.max_vida def estado(self): if self.vida > 0: return "Viva" + "\n" + str(self.vida) + "\n" elif self.vida <= 0: self.vida = 0 return "Muerta" + "\n" +str( self.vida) + "\n" Maeve = Personaje() Maeve.estado() Maeve.hit(200) Maeve.estado() Maeve.heal(500) Maeve.estado() Maeve.hit(1899) Maeve.estado() Maeve.heal(700) Maeve.estado() Maeve.hit(2000) Maeve.estado()
31st Oct 2017, 4:49 AM
jay
- 1
THESE ARE THE MISTAKES class Personaje(): def __init__(self): self.vida = 1900 self.max_vida = self.vida def hit(self,daƱo): self.vida -= self.daƱo #(self.vida -=self.dano) this is wrong, you can do (self.dano = dano) # or (self.vida -=dano) without having to create a new instance member . if self.vida <= 0: # you don't need the "=" just "<" is enough self.vida = 0 def heal(self,healing): if self.vida >= 0: self.vida += self.healing # samething in here you're making a new instance member.this could # cause a lot of truble. if self.vida > self.max_vida: self.vida = self.max_vida def estado(self): if self.vida > 0: return "Viva" + "\n" + str(self.vida) + "\n" # YOU CAN'T add a string and an "intager" # (self.vida). you can do this str(self.vida) elif self.vida <= 0: self.vida = 0 return "Muerta" + "\n" +str( self.vida) + "\n" #same here can't adda a string and self.vida Maeve = Personaje() Maeve.estado() Maeve.hit(200) Maeve.estado() Maeve.heal(500) Maeve.estado() Maeve.hit(1899) Maeve.estado() Maeve.heal(700) Maeve.estado() Maeve.hit(2000) Maeve.estado()
31st Oct 2017, 4:46 AM
jay