move
is defined as an instance method (defined within the class definition), as every instance method receives a first mandatory parameter that is the instance of the class itself, which by convention is named by self
, this parameter is passed automatically when the method is called through an instance ( instancia.method()
). In your case move
is defined as:
def move(argument)
By not defining the parameter self
, when you make player.move("ri")
the instance of the class is automatically passed to the first argument that is argument
, in addition there is the parameter that you pass "ri"
, by so the function receives two parameters when it only has one defined, the reference to "ri"
has nowhere to go because argument
was already used by the reference to the instance player
.
Apart from this swither
you must define it or within move
or better in the __init__
as an instance attribute, apart the dictionary values are incorrect, they must 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
returns "Invalid Argument"
, so in the next line you try to call a string:
return "Invalid Argument"()
what you can do is something 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 have added a couple of properties to validate x
e y
in the instantiation and make sure they are between 1 and 5.
The output 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)