为什么我的封闭区域功能给出" TypeError:不支持的操作数类型+:' NoneType'和' NoneType' &#34 ;?

时间:2018-01-03 05:57:41

标签: python python-3.x typeerror python-3.6 nonetype

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

  • y = m1 * x + b1
  • y = m2 * x + b2
  • y = m3 * x + b3

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

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

以下是我遵循的方法(我是一个完整的新手,所以你的详细评论/解释会非常有帮助):

#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))

2 个答案:

答案 0 :(得分:0)

这里是您的代码失败的追溯:

$ 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'

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

    p = (D1 + D2 + D3)*0.5    

...所以,鉴于错误消息unsupported operand type(s) for +: 'NoneType' and 'NoneType',很明显至少有几个D1D2D3必须以某种方式None D1 = dist(x1,y1,x2,y2) D2 = dist(x1,y1,x3,y3) D3 = dist(x2,y2,x3,y3) 1}}。这些值在第72-74行创建:

dist()

...所以看起来def dist(x1,y1,x2,y2): return D = ((x1-x2)**2 + (y1-y2)**2) 是罪魁祸首。我们来看看:

return

而且,这是你的问题。在分配给D的行有机会运行之前,第27行的def dist(x1, y1, x2, y2): return ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5 行终止了函数and returns None。这是一个固定版本:

$ python3.6 heron.py
0.0

(请注意,我还添加了缺少的平方根操作,以便返回正确的结果)

该程序现在运行:

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

...打印0.0的区域,这是错误的。 的原因与您询问的异常不同,所以我会停在这里,除非提示您可能希望在这段代码中更仔细一点:

www.mydomain.com/app1 
www.mydomain.com/app2 

答案 1 :(得分:0)

第一次帮助功能:找到交叉点:

此函数采用四个表示两行的int或float值,并返回两行的交点的x值。如果这些行是平行的或相同的,则该函数应返回None。

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

    if m1 == m2:
        return None


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

第二次帮助功能:找到两点之间的距离:

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

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

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

第三次帮助功能:使用Heron公式

查找封闭区域

此函数采用三个表示三角形边长的int或float值,并使用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