在 2D 坐标平面中移动,跟踪位置和距原点的距离
Moving in 2D coordinate plane, keeping track of position and distance from the origin
所以我正在尝试编写一个代码(主要使用 class),让我可以让机器人从中心 (0,0) UP 开始移动(假设有一个坐标平面),向下、向左或向右。我们的任务是让程序反复要求输入一个字符串来识别移动的方向和距离,然后输出机器人的当前位置和距离原点的欧氏距离。我们假设方向和距离的输入由 space 分隔(例如“UP 5”,“LEFT 6”)。如果输入无效(例如没有方向或没有数字),打印“无效输入”。示例输出如下所示。
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...
这是我尝试过的解决方案:
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
当我尝试 运行 时,我总是得到这个:
Input DIRECTION AND DISTANCEUP 5
<__main__.Robot object at 0x000001EB1DEA9F60>
有人能解释一下我做错了什么吗?
首先,您需要创建 class 机器人的对象才能使用其方法。这称为面向对象编程 (OOP),我建议阅读它的一些基础知识。
对于这个特定问题,您不需要 OOP 解决方案。
删除 class 定义并简单地使用函数本身:
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
请注意,要调用移动方法,您必须像这样提供输入参数:
运动(a[0],a[1])
编辑:
好的,在注意到您必须有一个 OOP 解决方案之后,我编写了这段代码。这正是您所需要的:
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
首先我做了另外两个函数。一种用于距离,一种用于位置。
移动功能不应输出任何内容。 OOP 的要点是简化一切。对于每个特定的操作,您都需要一个特定的方法(即函数)。
您在 class 中定义这些方法,然后创建该 class 的对象。并将函数 "via" 称为您创建的对象。 Class 就像构建机器人的指令。而对象是你的机器人。您可以根据需要制作任意数量的对象(机器人)。每个对象就像一个变量,保存您在 class 定义中写入的数据。 (参数和方法)
另外,a[1] 始终是字符串。 (尽管您在 if 条件中检查了它,但它不是数字,但这意味着可以将字符串视为数值。数字不同于 int 类型)。当我将它发送到移动方法时,我不得不将它投射到浮动。
(我不想将它转换为 int,因为距离值可能是像 0.6 这样的浮点值)
互联网上有很好的 OOP 资源。最简单的是 w3schools。
https://www.w3schools.com/python/python_classes.asp
所以我正在尝试编写一个代码(主要使用 class),让我可以让机器人从中心 (0,0) UP 开始移动(假设有一个坐标平面),向下、向左或向右。我们的任务是让程序反复要求输入一个字符串来识别移动的方向和距离,然后输出机器人的当前位置和距离原点的欧氏距离。我们假设方向和距离的输入由 space 分隔(例如“UP 5”,“LEFT 6”)。如果输入无效(例如没有方向或没有数字),打印“无效输入”。示例输出如下所示。
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...
这是我尝试过的解决方案:
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
当我尝试 运行 时,我总是得到这个:
Input DIRECTION AND DISTANCEUP 5
<__main__.Robot object at 0x000001EB1DEA9F60>
有人能解释一下我做错了什么吗?
首先,您需要创建 class 机器人的对象才能使用其方法。这称为面向对象编程 (OOP),我建议阅读它的一些基础知识。
对于这个特定问题,您不需要 OOP 解决方案。
删除 class 定义并简单地使用函数本身:
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
请注意,要调用移动方法,您必须像这样提供输入参数: 运动(a[0],a[1])
编辑: 好的,在注意到您必须有一个 OOP 解决方案之后,我编写了这段代码。这正是您所需要的:
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
首先我做了另外两个函数。一种用于距离,一种用于位置。 移动功能不应输出任何内容。 OOP 的要点是简化一切。对于每个特定的操作,您都需要一个特定的方法(即函数)。
您在 class 中定义这些方法,然后创建该 class 的对象。并将函数 "via" 称为您创建的对象。 Class 就像构建机器人的指令。而对象是你的机器人。您可以根据需要制作任意数量的对象(机器人)。每个对象就像一个变量,保存您在 class 定义中写入的数据。 (参数和方法)
另外,a[1] 始终是字符串。 (尽管您在 if 条件中检查了它,但它不是数字,但这意味着可以将字符串视为数值。数字不同于 int 类型)。当我将它发送到移动方法时,我不得不将它投射到浮动。 (我不想将它转换为 int,因为距离值可能是像 0.6 这样的浮点值)
互联网上有很好的 OOP 资源。最简单的是 w3schools。 https://www.w3schools.com/python/python_classes.asp