Python 当我想添加两个整数时,函数一直认为我在添加方法
我一直遇到这个错误:Python 当我想添加两个整数时,函数一直认为我在添加方法,python,Python,我一直遇到这个错误: class Line: '''Fields: slope(anyof Int Float "undefined"), intercept (anyof Int Float) ''' def __init__(self,slope,intercept): self.slope = slope self.intercept = intercept def __repr__(self):
class Line:
'''Fields: slope(anyof Int Float "undefined"), intercept (anyof Int Float)
'''
def __init__(self,slope,intercept):
self.slope = slope
self.intercept = intercept
def __repr__(self):
s1 = "Y = {0:.2f}X + {1:.2f}"
s2 = "X = {0:.2f}"
s3 = "Y = {0:.2f}"
if self.slope=="undefined":
return s2.format(self.intercept)
elif self.slope==0:
return s3.format(self.intercept)
else:
return s1.format(self.slope, self.intercept)
def __eq__(self, other):
return type(other) == type(self) and self.slope == other.slope and \
self.intercept == other.intercept
def intersect(self, other):
'''
intersect(self,other) return a list that contains two points that
represent the intersection of the two lines. False is returned if the
line are parallel or equal.
intersect: Line((anyof Int Str Float),(anyof Float Int)
Line((anyof Int Str Float),(anyof Float Int)
-> (anyof Bool (listof (anyof Int Float) (anyof Int Float)))
Examples:
L1 = Line(1,3)
L2 = Line(-1,3)
L1.intersect(L2) => [0,3]
L1 = Line(1,3)
L2 = Line(1,3)
L1.intersect(L2) => False
'''
if self.slope == other.slope:
return False
if self.slope == 0:
if other.slope == 'undefined':
return [other.intersect,self.intersect]
if other.slope == 0:
if self.slope == 'undefined':
return [self.intersect,other.intersect]
if self.slope == 'undefined':
x = self.intersect
y = (other.slope * x) + other.intersect
return [x,y]
if other.slope == 'undefined':
x = other.intersect
y = (self.slope * x) + self.intersect
return [x,y]
if self.slope != other.slope:
x = (other.intersect - self.intersect)/(self.slope - other.slope)
y = (self.slope * x) + self.intersect
return[x,y]
L5 = Line(1,10)
L6 = Line(0,5)
L5.intersect(L6)
请帮忙我想你想做的是: 在最后一个if中,不是intersect(实际上是一种方法),而是减去intercept,这是一个值 没有
请粘贴整个异常,回溯显示它在哪一行,而不仅仅是它的描述。但是
intersect
是一种方法。如果你想得到返回值,你需要调用它。我想我明白我做错了什么。我一直在放intersect而不是intersect。谢谢你,Ignacio Vazquez AbramsYou可能会考虑重命名这两个来让他们不那么相似。例如,y\u intercept
和get\u intersection
。
builtins.TypeError: unsupported operand type(s) for -: 'method' and 'method'
if self.slope != other.slope:
x = (other.intercept - self.intercept)/(self.slope - other.slope)
y = (self.slope * x) + self.intercept
return[x,y]