TypeError: unsupported operand type(s) for ** or pow(): 'NoneType' and 'int'

128 views Asked by At

Can someone please answer my question? Why am I getting the error message "TypeError: unsupported operand type(s) for ** or pow(): 'NoneType' and 'int'", when I run the following code?

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)

Please find time to reply!

I wanted to write code for calculating the area of a circle as present in Allen Downey's book. I first defined a function 'distance' using the distance formula. Then, I defined a function 'area' using radius as an argument. Then I created a third function for calculating the area of the circle calling the previous two functions, distance and area but I think Python is treating my distance function as a Nonetype variable or something.

1

There are 1 answers

1
richyen On

You need to use return so that subsequent functions can make use of the calculations you made in distance and 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)