Click here to Skip to main content
15,907,497 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why am I getting the error message "TypeError: unsupported operand type(s) for ** or pow(): 'NoneType' and 'int'", when I run the following code?

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


What I have tried:

I am trying to calculate the area of a circle. I created a function for distance and for area and then I called both of them within another function, area_circle. But, I think there that the distance function has type None. There is something wrong with the r variable which I have assigned to the function distance or there is something wrong with the distance function itself.
Posted
Updated 7-Oct-23 20:55pm
v2

1 solution

Neither of your distance or area functions return a value:, but the code calling the functions expects them to.

See here: Python Function Return Value[^] and try this:
Python
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)
    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)
    return(result)

print(area_circle(1,2,3,6))
Be aware though that you will get "odd" results: your area does not have to be square so your circle may be more of an ellipse or will exceed the boundaries of your given area.
 
Share this answer
 
Comments
Baziga 8-Oct-23 5:38am    
Really grateful to you! Thank you so much!
OriginalGriff 8-Oct-23 6:01am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900