在 Python 中计算斜率的方法
Method That Calculates Slope In Python
我正尝试在 Python 中学习面向对象编程。为此,我需要创建一种方法来计算一条线的斜率,该线将原点连接到一个点。 (我认为)我们假设原点是 (0,0)。例如:
Point(4, 10).slopeFromOrigin()
2.5
Point(12, -3).slopeFromOrigin()
-0.25
Point(-6, 0).slopeFromOrigin()
0
我们使用方程 slope = (Y2 - Y1) / (X2 - X1)
来计算斜率。此外,由于不允许除以 0,因此我们需要在方法失败时 return None
。这是我尝试过的:
class Point:
#Point class for representing and manipulating x,y coordinates
def __init__(self, initX, initY):
#Create a new point at the given coordinates
self.x = initX
self.y = initY
def getX(self):
return self.x
def getY(self):
return self.y
def distanceFromOrigin(self):
return ((self.x ** 2) + (self.y ** 2)) ** 0.5
#define a method called slopeFromOrigin here
def slopeFromOrigin(self):
#set origin values for x and y (0,0)
self.x = 0
self.y = 0
#slope = (Y2 - Y1) / (X2 - X1)
if (Point(x) - self.x) == 0:
return None
else:
return (Point(y) - self.y) / (Point(x) - self.x)
#some tests to check our code
from test import testEqual
testEqual( Point(4, 10).slopeFromOrigin(), 2.5 )
testEqual( Point(5, 10).slopeFromOrigin(), 2 )
testEqual( Point(0, 10).slopeFromOrigin(), None )
testEqual( Point(20, 10).slopeFromOrigin(), 0.5 )
testEqual( Point(20, 20).slopeFromOrigin(), 1 )
testEqual( Point(4, -10).slopeFromOrigin(), -2.5 )
testEqual( Point(-4, -10).slopeFromOrigin(), 2.5 )
testEqual( Point(-6, 0).slopeFromOrigin(), 0 )
如你所见,我想说的是,我们需要 Point 的第一个参数是 x2,Point 的第二个参数是 y2。我这样试了一下,得到了
NameError: name 'y' is not defined on line 32
。
我也试过这样获取Point的索引值:
return (Point[0] - self.y / (Point[1] - self.x)
但这也给了我一个错误信息:
TypeError: 'Point' does not support indexing on line 32
我不确定如何从 Point
中获取 x 和 y 参数的值,以便该方法在测试时有效。如果您有任何建议,请分享您的建议。谢谢你。
第一个问题
self.x = 0
self.y = 0
您刚刚将当前点设置为原点。不要那样做。到原点的距离将为 0...
第二个问题 Point(x)
和 Point(y)
不是您获取 self.x
和 self.y
值的方式。
那么,坡度就是"rise over run"。另外你想 return None
当 self.x == 0
。
所以,简单地说
def slopeFromOrigin(self):
if self.x == 0:
return None
return self.y / self.x
甚至
def slopeFromOrigin(self):
return None if self.x == 0 else self.y / self.x
或者让PythonreturnNone自己
def slopeFromOrigin(self):
if self.x != 0:
return self.y / self.x
我认为你的困惑在于你认为你需要以某种方式定义 "the origin"。如果您需要这样做,您可以使用
origin = Point(0,0)
Point(-6, 0).slopeFromPoint(origin)
if (Point(x) - self.x) == 0:
return None
else:
return (Point(y) - self.y) / (Point(x) - self.x)
As you can see, I'm trying to say that we need the first parameter of Point to be x2, and the second parameter of Point to be y2. I tried it this way and got
NameError: name 'y' is not defined on line 32.
您正在尝试访问 y
的值,这是一个您尚未分配的全局变量。
I also tried to get the index values of Point like this:
return (Point[0] - self.y / (Point[1] - self.x)
两个问题:
- "Point" 是一个 class,不是一个对象(它是一个对象的实例)。
即使您放置了一个对象,Point 也不是一个类似列表的对象。为了使用 variableName[index]
之类的索引访问项目,variableName
的 class 必须具有 __getitem__(self, key)
的实现。例如:
>>> class GoodListClass:
... def __init__(self, list):
... self.myList = list
... def __getitem__(self, key):
... return self.myList[key]
...
>>> class BadListClass:
... def __init__(self, list):
... self.myList = list
...
>>> someList = range(10)
>>> goodListObject = GoodListClass(someList)
>>> badListObject = BadListClass(someList)
>>> print(goodListObject[2])
2
>>> print(badListObject[2])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: BadListClass instance has no attribute '__getitem__'
我正尝试在 Python 中学习面向对象编程。为此,我需要创建一种方法来计算一条线的斜率,该线将原点连接到一个点。 (我认为)我们假设原点是 (0,0)。例如:
Point(4, 10).slopeFromOrigin()
2.5
Point(12, -3).slopeFromOrigin()
-0.25
Point(-6, 0).slopeFromOrigin()
0
我们使用方程 slope = (Y2 - Y1) / (X2 - X1)
来计算斜率。此外,由于不允许除以 0,因此我们需要在方法失败时 return None
。这是我尝试过的:
class Point:
#Point class for representing and manipulating x,y coordinates
def __init__(self, initX, initY):
#Create a new point at the given coordinates
self.x = initX
self.y = initY
def getX(self):
return self.x
def getY(self):
return self.y
def distanceFromOrigin(self):
return ((self.x ** 2) + (self.y ** 2)) ** 0.5
#define a method called slopeFromOrigin here
def slopeFromOrigin(self):
#set origin values for x and y (0,0)
self.x = 0
self.y = 0
#slope = (Y2 - Y1) / (X2 - X1)
if (Point(x) - self.x) == 0:
return None
else:
return (Point(y) - self.y) / (Point(x) - self.x)
#some tests to check our code
from test import testEqual
testEqual( Point(4, 10).slopeFromOrigin(), 2.5 )
testEqual( Point(5, 10).slopeFromOrigin(), 2 )
testEqual( Point(0, 10).slopeFromOrigin(), None )
testEqual( Point(20, 10).slopeFromOrigin(), 0.5 )
testEqual( Point(20, 20).slopeFromOrigin(), 1 )
testEqual( Point(4, -10).slopeFromOrigin(), -2.5 )
testEqual( Point(-4, -10).slopeFromOrigin(), 2.5 )
testEqual( Point(-6, 0).slopeFromOrigin(), 0 )
如你所见,我想说的是,我们需要 Point 的第一个参数是 x2,Point 的第二个参数是 y2。我这样试了一下,得到了
NameError: name 'y' is not defined on line 32
。
我也试过这样获取Point的索引值:
return (Point[0] - self.y / (Point[1] - self.x)
但这也给了我一个错误信息:
TypeError: 'Point' does not support indexing on line 32
我不确定如何从 Point
中获取 x 和 y 参数的值,以便该方法在测试时有效。如果您有任何建议,请分享您的建议。谢谢你。
第一个问题
self.x = 0
self.y = 0
您刚刚将当前点设置为原点。不要那样做。到原点的距离将为 0...
第二个问题 Point(x)
和 Point(y)
不是您获取 self.x
和 self.y
值的方式。
那么,坡度就是"rise over run"。另外你想 return None
当 self.x == 0
。
所以,简单地说
def slopeFromOrigin(self):
if self.x == 0:
return None
return self.y / self.x
甚至
def slopeFromOrigin(self):
return None if self.x == 0 else self.y / self.x
或者让PythonreturnNone自己
def slopeFromOrigin(self):
if self.x != 0:
return self.y / self.x
我认为你的困惑在于你认为你需要以某种方式定义 "the origin"。如果您需要这样做,您可以使用
origin = Point(0,0)
Point(-6, 0).slopeFromPoint(origin)
if (Point(x) - self.x) == 0:
return None
else:
return (Point(y) - self.y) / (Point(x) - self.x)
As you can see, I'm trying to say that we need the first parameter of Point to be x2, and the second parameter of Point to be y2. I tried it this way and got
NameError: name 'y' is not defined on line 32.
您正在尝试访问 y
的值,这是一个您尚未分配的全局变量。
I also tried to get the index values of Point like this:
return (Point[0] - self.y / (Point[1] - self.x)
两个问题:
- "Point" 是一个 class,不是一个对象(它是一个对象的实例)。
即使您放置了一个对象,Point 也不是一个类似列表的对象。为了使用
variableName[index]
之类的索引访问项目,variableName
的 class 必须具有__getitem__(self, key)
的实现。例如:>>> class GoodListClass: ... def __init__(self, list): ... self.myList = list ... def __getitem__(self, key): ... return self.myList[key] ... >>> class BadListClass: ... def __init__(self, list): ... self.myList = list ... >>> someList = range(10) >>> goodListObject = GoodListClass(someList) >>> badListObject = BadListClass(someList) >>> print(goodListObject[2]) 2 >>> print(badListObject[2]) Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: BadListClass instance has no attribute '__getitem__'