因此,我正在尝试编写一个代码(主要使用类),该代码使我可以使机器人从中心(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>
有人会解释我在做什么错吗?
答案 0 :(得分:1)
首先,您需要使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
请注意,要调用移动方法,您必须提供如下输入参数: 运动(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的重点是简化一切。对于每个特定的动作,您都需要特定的方法(即函数)。
您可以在类中定义这些方法,然后创建该类的对象。并通过您创建的对象“调用”函数。类就像建造机器人的指令。而对象是您的机器人。您可以根据需要制作任意数量的对象(机器人)。每个对象就像一个变量,用于保存您在类定义中编写的数据。 (参数和方法)
a [1]始终是字符串。 (尽管您在if条件中进行了检查,但它不是数字,但这意味着可以将字符串视为数字值。数字与int类型不同)。当我将其发送到移动方法时,我不得不将其转换为浮动。 (我不想将其强制转换为int,因为距离值可能是一个像0.6这样的浮点值)
Internet上有很多有关OOP的资源。最简单的一种是w3schools。 https://www.w3schools.com/python/python_classes.asp