E
move is defined as a method of instance (defined within the definition of the class), as any method of instance receives a first mandatory parameter which is the instance of the class itself, which by convention is named by self, this parameter is automatically passed when the method is called using an instance (instancia.method()). In your case move is defined as:def move(argument)
By not defining the parameter selfWhen you do player.move("ri") you pass the class instance automatically to the first argument that is argument, plus there's the parameter you pass it "ri", so the function receives two parameters when it only has one defined, the reference to "ri" He has nowhere to go because argument was already used by reference to the instance player.Apart from this swither You must define it or within move or better at it __init__ as an instance attribute, apart from the dictionary values are incorrect, should be key: self.metodo.Finally;func = switcher.get(argumen, "Invalid Argument")
return func()
will fail if the key is not in the dictionary, if this happens get return "Invalid Argument"So on the next line you try to call a string:return "Invalid Argument"()
What you can do is like this:func = self._switcher.get(argumen)
if func is None:
return "Invalid Argument"
return func()
class Robot:
def __init__(self, x, y):
self.x = x
self.y = y
self._switcher = {
"le": self.move_left,
"ri": self.move_right,
"up": self.move_up,
"do": self.move_down,
}
@property
def x(self):
return self._x
@x.setter
def x(self, x_value):
if not 1 <= x_value <= 5:
raise ValueError("x must be between 1 and 5 (both included)")
self._x = x_value
@property
def y(self):
return self._y
@y.setter
def y(self, y_value):
if not 1 <= y_value <= 5:
raise ValueError("y must be between 1 and 5 (both included)")
self._y = y_value
def move_left(self):
if (self._x == 1):
return "You are in the left limit"
else:
self._x -= 1
return "({},{})".format(self._x, self._y)
def move_right(self):
if (self._x == 5):
return "You are in the rigth limit"
else:
self._x += 1
return "({},{})".format(self._x, self._y)
def move_up(self):
if (self._y == 1):
return "You are in the up limit"
else:
self._y -= 1
return "({},{})".format(self._x, self._y)
def move_down(self):
if (self._y == 5):
return "You are in the down limit"
else:
self._y += 1
return "({},{})".format(self._x, self._y)
def move(self, argumen):
func = self._switcher.get(argumen)
if func is None:
return "Invalid Argument"
return func()
def main():
player = Robot(1, 1)
print(player.move("ri"))
print(player.move("ri"))
print(player.move("ri"))
print(player.move("ri"))
print(player.move("ri"))
print(player.move("do"))
print(player.move("up"))
print(player.move("up"))
print(player.move("le"))
print(player.move("fo"))
player2 = Robot(6, 1)
if name == "main":
main()
I added a couple of properties to validate x e y in the instance and make sure they are between 1 and 5.The exit for that main is:(2,1)
(3,1)
(4,1)
(5,1)
You are in the rigth limit
(5,2)
(5,1)
You are in the up limit
(4,1)
Invalid Argument
Traceback (most recent call last):
File "test.py", line 84, in <module>
main()
File "test.py", line 81, in main
player2 = Robot(6, 1)
File "test.py", line 4, in init
self.x = x
File "test.py", line 20, in x
raise ValueError("x must be between 1 and 5 (both included)")
ValueError: x must be betwen 1 and 5 (both included)