在 2D 坐标平面中移动,跟踪位置和与原点的距离



所以我正在尝试制作一个代码(主要使用class(,允许我使机器人从中心(0,0(向上,向下,向左或向右移动(想象有一个坐标平面(。我们的任务是让程序反复请求字符串输入以识别方向和距离移动,然后输出机器人与原点的当前位置和欧氏距离。我们假设方向和距离的输入由空格分隔(例如"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>

有人会解释我做错了什么吗?

首先,您需要创建类 Robot 的对象才能使用其方法。这被称为面向对象编程(OOP(,我建议阅读它的一些基础知识。

对于此特定问题,您不需要 OOP 解决方案。

删除类定义并简单地使用函数本身:

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

请注意,要调用 movement 方法,您必须给出如下输入参数: 机芯(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

首先,我做了另外 2 个函数。一个用于距离,一个用于位置。 移动函数不应输出任何内容。关于OOP的重点是简化一切。对于每个特定操作,您都需要一个特定的方法(即函数(。

在类中定义这些方法,然后创建该类的对象。并调用函数"通过"您创建的对象。类就像构建机器人的指令。而物体是你的机器人。您可以根据需要制作任意数量的对象(机器人(。每个对象都像一个变量,用于保存您在类定义中编写的数据。(参数和方法(

此外,a[1] 始终是字符串。(尽管您在 if 条件中检查了它,但它不是数字,但这意味着字符串可以被视为数值。数字不同于 int 类型(。我不得不把它投到漂浮,同时我把它送到移动方法。 (我不想将其转换为 int,因为距离值可能是像 0.6 这样的浮点值(

互联网上有关于OOP的好资源。最简单的一个是w3schools。 https://www.w3schools.com/python/python_classes.asp

您将设计一个程序,允许机器人从原始点 (0, 0( 开始在 2D 坐标系中移动。机器人可以移动到用户指定的下一个点(x,y(。如果下一个点是(999,999(,程序结束并打印出每一步移动的距离。

最新更新