Moving in 2D coordinate plane, keeping track of position and distance from the origin

Question:

So i’m trying to make a code (mainly using class) that would allow me to make a robot move (imagine there’s a coordinate plane) starting from the center (0,0) UP, DOWN, LEFT or RIGHT. We were task to have the program repeatedly ask for a string input to identify the direction and distance movement, and then output the current position and euclidean distance of the robot from the origin. We shall assume that the input for the direction and distance is separated by a space (e.g. “UP 5”, “LEFT 6”). And if the input is invalid (e.g. no direction or no number), print “Invalid Input”. Sample output is shown below.

Input (Direction Distance): UP 5
Position = (0, 5.0)
Distance = 5.0

Input (Direction Distance): LEFT 2
Position = (-2.0, 5.0)
Distance = 5.3851...

Here is the solution that i tried:

class Robot(object):
    def __init__(self, n=(0,0)):
        self.n = n
        post = list(n)
        self.x = post[0]
        self.y = post[1]

    def movement(self,a):
        direction = self.a[0]
        move = self.a[1]
        if direction == "UP":
            y_d = self.y + move.y
        elif direction == "DOWN":
            y_d = self.y - move.y
        elif direction == "RIGHT":
            x_d = self.x + move.x
        elif direction == "LEFT":
            x_d = self.x - move.x
        return (x_d**2 + y_d**2) **0.5


direction_list = ["UP","DOWN","LEFT","RIGHT"]      
while True:
    question = input('Do you want to enter a move? (YES/NO)')
    while question == 'YES' or question == "Yes" or question == "yes":
        a = input('Input DIRECTION AND DISTANCE').split()
        if a[0] in direction_list and a[1].isnumeric():
            #direction = a[0]
            #distance = a[1]
            print(Robot(a))
        else: 
            print('Invalid Input')  
    break

When i try to run it, i always get this:

Input DIRECTION AND DISTANCEUP 5
<__main__.Robot object at 0x000001EB1DEA9F60>

Will somebody explain what i’m doing wrong?

Asked By: user11589840

||

Answers:

First you need to make an object of the class Robot to be able to use its methods. This is called object oriented programming (OOP) I suggest read some of the basics of it.

For this specific problem you don’t need an OOP solution.

Remove the class definition and simply use the function itself :

 def movement(direction,move):
        if direction == "UP":
            # mathematical calculations without using self. Just put the numbers or 
            #variables you have defined before 
            dis = direction * 10 + move/ 10 # just for sake of having an output
        elif direction == "DOWN":
            dis = direction * 10 + move/ 10 # just for sake of having an output
        elif direction == "RIGHT":
            dis = direction * 10 + move/ 10 # just for sake of having an output
        elif direction == "LEFT":
            dis = direction * 10 + move/ 10 # just for sake of having an output
        return dis

direction_list = ["UP","DOWN","LEFT","RIGHT"]      
while True:
    question = input('Do you want to enter a move? (YES/NO)')
    while question == 'YES' or question == "Yes" or question == "yes":
        a = input('Input DIRECTION AND DISTANCE').split()
        if a[0] in direction_list and a[1].isnumeric():
            #direction = a[0]
            #distance = a[1]
            print(movement(a[0], a[1]))
        else: 
            print('Invalid Input')   
    break

Notice that to call the movement method you have to give the input arguments like this :
movement(a[0], a[1])


Edit:
Okay after noticing that you have to have a OOP solution I made this piece of code. Which is exactly what you need:

class Robot(object):
    def __init__(self, n=(0,0)):
        self.n = n
        post = list(n)
        self.x = post[0]
        self.y = post[1]
    def distance(self):
        return (self.x **2 + self.y **2) **0.5

    def position(self):
        print("x: ", self.x,"y: ", self.y)

    def movement(self,direction,move):
        x_d = self.x
        y_d = self.y
        if direction == "UP":
            y_d = self.y + move
        elif direction == "DOWN":
            y_d = self.y - move
        elif direction == "RIGHT":
            x_d = self.x + move
        elif direction == "LEFT":
            x_d = self.x - move
        self.x = x_d
        self.y = y_d
        return 0



direction_list = ["UP","DOWN","LEFT","RIGHT"]
robot = Robot()


while True:
    question = input('Do you want to enter a move? (YES/NO)')
    while question == 'YES' or question == "Yes" or question == "yes":
        a = input('Input DIRECTION AND DISTANCE').split()
        if a[0] in direction_list:
            #direction = a[0]
            #distance = a[1]
            robot.movement(a[0], float(a[1]))
            print("Position: ")
            robot.position()
            print ("distance ",robot.distance())
        else: 
            print('Invalid Input')  
    break

First I made 2 other functions. One for distance and one for position.
The movement function should not output anything. The point about OOP is to simplify everything. For every specific action you need a specific method (i.e function).

You define these methods in your class and then make an object of that class. And call the functions “via” the object you made. Class is like an instruction for building a robot. While object is your robot. You can make as many object(robots) as you like. Each object is like a variable that holds the data you have written in the class definition. (Arguments and methods)

Also a[1] is always string. (it is not numerical although you checked it in your if condition but that means that a string can be seen as a numerical value. Being numerical is different from being an int type). I had to cast it to float while I sent it to the movement method.
(I didn’t want to cast it to int because the distance value might be an float value like 0.6)

There are good resources on OOP on the internet. The simplest one is w3schools.
https://www.w3schools.com/python/python_classes.asp

Answered By: Ball Hog

You are going to design a program which allows a robot to move in a 2D coordinate system starting from the original point (0, 0). The robot can move to the next point (x, y) specified by the user. If the next point is (999, 999), the program ends and prints out the distance moved at each step.

Answered By: harry