TypeError:** 或 pow() 不支持的操作数类型:'NoneType' 和 'int'

问题描述 投票:0回答:1

有人可以回答我的问题吗?当我运行以下代码时,为什么会收到错误消息“TypeError: unsupported operand type(s) for ** or pow(): 'NoneType' and 'int'”?

import math

def distance (x1,y1,x2,y2):
    horizontal=x2-x1
    vertical=y2-y1
    horizontal_squared=horizontal**2
    vertical_squared=vertical**2
    distance=math.sqrt(horizontal_squared+vertical_squared)
    print(distance)

def area(radius):
     math.pi*radius**2

def area_circle(xc,yc,xp,yp):
    r=distance(xc,yc,xp,yp)
    result=area(r)
    print (result)

area_circle(1,2,3,6)

请抽时间回复!

我想编写计算圆面积的代码,如艾伦唐尼的书中所示。我首先使用距离公式定义了一个函数“距离”。然后,我使用半径作为参数定义了一个函数“区域”。然后我创建了第三个函数来计算圆的面积,调用前两个函数,距离和面积,但我认为 Python 将我的距离函数视为 Nonetype 变量或其他东西。

python function distance nonetype operands
1个回答
0
投票

您需要使用

return
,以便后续函数可以利用您在
distance
area
中进行的计算:


def distance (x1,y1,x2,y2):
    horizontal=x2-x1
    vertical=y2-y1
    horizontal_squared=horizontal**2
    vertical_squared=vertical**2
    distance=math.sqrt(horizontal_squared+vertical_squared)
    print(distance)
    return distance

def area(radius):
     return math.pi*radius**2

def area_circle(xc,yc,xp,yp):
    r=distance(xc,yc,xp,yp)
    result=area(r)
    print (result)

area_circle(1,2,3,6)
© www.soinside.com 2019 - 2024. All rights reserved.