有人可以回答我的问题吗?当我运行以下代码时,为什么会收到错误消息“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 变量或其他东西。
您需要使用
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)