+:' int'不支持的操作数类型和' classobj'

时间:2014-04-12 16:32:36

标签: python

我正在尝试从两条线的交叉点找到坐标,并且在相交def处我有这个错误:

unsupported operand type(s) for +: 'int' and 'classobj' 

我能做些什么来解决它?

class Point:
    def __init__(self,x,y):
        self.x=x
        self.y=y

class Line1:
    def __init__(self,a1,b1,c1):
        self.a1=a1
        self.b1=b1
        self.c1=c1
    def slope(self):
        try:
            return -self.a1/self.b1
        except ZeroDivisionError:
            return None
    def isonline(self,Point):
        if (self.a1*Point.x+self.b1*Point.y+self.c1)==0:
            return True
        else:
            return False

class IntersectionPoint:
    def __init__(self,a2,b2,c2):
        self.a2=a2
        self.b2=b2
        self.c2=c2            
    def intersect(self,Line1): 
        xcord=(-Line1.a1/Line1.b1)+(self.a2/self.b2)-Line1.c1+self.c2
        ycord=(-Line1.a1/Line1.b1)*xcord-self.c2
        return 'True. The intersection point is: I' (xcord,ycord) 


coordinatesPoint=Point(1,1)
abcfromLine=Line1(2,-1 ,-3)
Line2=IntersectionPoint(3,-1 -1, Line1)
print abcfromLine.slope()
print abcfromLine.isonline(coordinatesPoint)
print Line2.intersect(abcfromLine)

2 个答案:

答案 0 :(得分:1)

您正在将类对象传递给IntersectionPoint()

Line2=IntersectionPoint(3,-1 -1, Line1)

Line1是一个班级。它是函数的第三个参数,因为两个-1参数之间没有逗号。因此,您已将3分配给a2,将-2分配给b2,将Line1分配给c2

然后在intersect方法中,将该类对象添加到整数:

Line1.c1+self.c2

其中self.c2是您的Line1类。

你想:

Line2 = IntersectionPoint(3, -1, -1)

代替。

接下来,您将收到此行的错误:

return 'True. The intersection point is: I' (xcord,ycord) 

因为这就像尝试将字符串用作函数一样。您要么缺少字符串格式化操作,要么缺少逗号:

return 'True. The intersection point is: (%d, %d)' % (xcord,ycord) 

return 'True. The intersection point is: I', (xcord,ycord) 

答案 1 :(得分:0)

更改

Line2=IntersectionPoint(3,-1 -1, Line1)

Line2=IntersectionPoint(3,-1, -1)

你传递了4个参数并且缺少逗号