I am trying to perform a task that includes the following steps :
#two. Create a Robot class
#2.1 Add constructor, attributes (5)
#2.2 Add methods (3)
#2.3 Add Setters
#2.4 Allow the robot to lose battery power for every kilometer traveled.
#2.5 Allow the robot to shoot and run out of bullets.
#2.6 If the robot is below 5 bullets or below 10% battery, enter save mode
but when starting the code it gives me an error because there are missing arguments in the class "self" but I can't detect what I am missing, can anyone help me?
(the error I get is that when I want to perform the walk function, the function should remove 5 from battery and print "The robot walked one kilometer" and then keep running the other functions.)
class Robot:
#2.1 Constructor
def init (self):
self.nombre=None
self.bateria=100
self.color=None
self.balas=34
self.brazos=0
#2.2 Métodos
def recorrer(self):
self.bateria-=5
print('El robot recorrió un kilómetro; su bateria ahora es de ',self.bateria,'%')
self.bateria<=4 or self.balas<=9
print('Ahorro de energía activado.')
def desparar(self):
self.balas-=1
print('El robot desparó, ahora tiene', self.balas, 'balas')
#2.3 Setters
def cambiar_nombre(self, nom):
self.nombre=nom
print('El robot se llama', nom)
def cambiar_brazos(self, num):
self.brazos=num
print('El robot tiene', num, 'brazos')
def cambiar_bateria(self, bateria):
self.bateria=bateria
print('Su bateria ahora es de', self.bateria, '%')
robot_7=Robot
robot_7.recorrer()
robot_7.cambiar_nombre('Alex')
I get this error, but I don't know how to fix it:
Traceback (most recent call last):
line 36, in <module>
robot_7.recorrer()
TypeError: Robot.recorrer() missing 1 required positional argument: 'self'
The first error is:
This leaves the variable
robot_7
pointing to the Robot class ; not to an object of the class.What you need is to instantiate (create) an object of the class. That's why you put the parentheses:
Second, the method
__init__()
carries double underscores on both sides.correct in
show
produces: