Why is my enclosed area function giving "TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType' "?

Why is my enclosed area function giving "TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType' "?

我想编写一个函数 func(m1, b1, m2, b2, m3, b3),它采用六个 int 或 float 值来表示 3 行 ,如下所示:

… 和 return 这些线所包围的区域。虽然我相信我的方法没问题,但出于某种原因我不断收到此 NoneType 错误:

TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType'

以下是我遵循的方法(我是一个完全的新手,所以你的详细 [​​=30=] 会很有帮助):

#First helping function: FIND POINTS OF INTERSECTION
#---------------------------------------------------------------------
#This function takes four int or float values representing two lines 
#and returns the x value of the point of intersection of the two lines. 
#If the lines are parallel, or identical, the function should return 
#None. 
#---------------------------------------------------------------------

def f(m1,b1,m2,b2):

    if m1 == m2:  
        return None

    else:
        return (b2 - b1)/(m1 - m2)   



#Second helping function: FIND LENGTH (DISTANCE BETWEEN POINTS)
#--------------------------------------------------------------------
#This function takes four int or float values representing two points 
#and returns the distance between those points.
#--------------------------------------------------------------------

def dist(x1,y1,x2,y2):

    return 

    D = ((x1-x2)**2 + (y1-y2)**2)


#3RD Helping function:  FIND AREA ENCLOSED BY 3 LINES 
#-----------------------------------------------------------------
#This function takes three int or float values representing side 
#lengths of a triangle, and returns the area of that triangle using 
#Heron's formula
#-----------------------------------------------------------------    
def heron(D1,D2,D3): 

    p = (D1 + D2 + D3)*0.5    
    # where p is half the perimeter 
    area = (p*(p-D1)*(p-D2)*(p-D3))**0.5

    return area 



#Finally, the last function:
#-------------------------------------------------------------------
#This function makes use of the helping functions above and connects 
#everything together 
#-------------------------------------------------------------------

def func(m1,b1,m2,b2,m3,b3):

#Using 1st helping function:
#---------------------------
    x1  = f(m1, b1, m2, b2)
    x2  = f(m1, b1, m3, b3)
    x3  = f(m2, b2, m3, b3)

    #---

    y1 = m1*x1 + b1
    y2 = m2*x2 + b2
    y3 = m3*x3 + b3


 #Using 2nd helping function:
 #---------------------------

    D1 = dist(x1,y1,x2,y2)
    D2 = dist(x1,y1,x3,y3)
    D3 = dist(x2,y2,x3,y3)


#Using 3rd helping function:
#---------------------------
    return heron(D1,D2,D3)


print(func(0,20,-2,50,0.5,-10))

这是您的代码失败的回溯:

$ python3.6 heron.py 
Traceback (most recent call last):
  File "heron.py", line 82, in <module>
    print(func(0,20,-2,50,0.5,-10))
  File "heron.py", line 79, in func
    return heron(D1,D2,D3)
  File "heron.py", line 40, in heron
    p = (D1 + D2 + D3)*0.5    
TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType'

Tracebacks 可以帮助你解决很多问题(所以总是将它们包含在 Stack Overflow 问题中)。这个告诉你你的 TypeError 是在第 40 行提出的:

    p = (D1 + D2 + D3)*0.5    

... 因此,鉴于错误消息 unsupported operand type(s) for +: 'NoneType' and 'NoneType',很明显至少有几个 D1D2D3 必须以某种方式 None.这些值是在第 72-74 行创建的:

    D1 = dist(x1,y1,x2,y2)
    D2 = dist(x1,y1,x3,y3)
    D3 = dist(x2,y2,x3,y3)

… 所以看起来 dist() 是罪魁祸首。让我们看看:

def dist(x1,y1,x2,y2):

    return 

    D = ((x1-x2)**2 + (y1-y2)**2)

而且,这是你的问题。在分配给 D 的行完全有机会 运行 之前,第 27 行的 return 终止了函数 and returns None。这是一个固定版本:

def dist(x1, y1, x2, y2):
    return ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5

(请注意,我还添加了缺失的平方根运算,因此它 returns 是正确的结果)

节目现在运行s:

$ python3.6 heron.py
0.0

…打印0.0的面积,呃,错了。 that 的原因与您询问的异常不同,所以我将在此处停止,只是暗示您可能需要更仔细地查看这段代码:

#Using 1st helping function:
#---------------------------
    x1  = f(m1, b1, m2, b2)
    x2  = f(m1, b1, m3, b3)
    x3  = f(m2, b2, m3, b3)

第一个辅助功能:求交点:

此函数采用四个整数或浮点值表示两条线,returns 是两条线交点的 x 值。如果直线平行或相同,函数应该 return None。

def noParallel(m1,b1,m2,b2):

    if m1 == m2:
        return None


else:
    x = (b2 - b1)/(m1 - m2)
    return x

第二个帮助功能:计算两点之间的距离:

此函数采用四个 int 或 float 值表示两个点,returns 是这些点之间的距离。

def dist(x1,y1,x2,y2):

    z = (x1-x2)**2 + (y1-y2)**2
    return z**0.5 

第三个帮助功能:使用海伦公式求封闭区​​域

此函数采用表示三角形边长的三个整数或浮点值,return使用 Heron 公式计算该三角形的面积

def heron(D1,D2,D3): 

    p = (D1 + D2 + D3)*0.5    
    #where p is half the perimeter 
    area = (p*(p-D1)*(p-D2)*(p-D3))**0.5

    return area 

把所有东西放在一起:

def overall(m1,b1,m2,b2,m3,b3):  #Calling functions into functions 

    x1 = noParallel(m1,b1,m2,b2)
    x2 = noParallel(m2,b2,m3,b3)
    x3 = noParallel(m1,b1,m3,b3)

    print(x1)
    print(x2)
    print(x3)

以下 if 语句的目的是捕获线条未形成三角形的情况,通知用户并终止代码。如果没有这个 if 语句,代码就会崩溃,你会得到一个 TYPE ERROR 因为 (y = int * None) 当三条线不形成三角形时

 if x1 == None or x2 == None or x3 == None:
            print("Lines do not form a triangle")
            return None 

代入 y

    y1 = m1*x1 + b1  
    y2 = m3*x2 + b3  #OR y2 = m2*x2 + b2 
    y3 = m3*x3 + b3


    print(y1)
    print(y2)
    print(y3)

    D1 = dist(x1,y1,x2,y2)
    D2 = dist(x1,y1,x3,y3)
    D3 = dist(x2,y2,x3,y3)

    print(D1)
    print(D2)
    print(D3)

    overall = heron(D1,D2,D3)
    return overall

    print(overall(0,10,-4,100,4,-100))

Area enclosed by three lines