+ 1

How do i calculate the area of a circle?

So, i'm programming in python 2.7 version(if the version is important, maybe the solution would be different in python 3 or above). Im doing an excercise that needs me to calculate an area of a circle. The code i have at this moment is something that should lead me to the solution, but i'm encountering all sorts of errors. The code goes like this import math def distance(x1, y1, x2, y2): dx=x2-x1 dy=y2-y1 dsquared=dx**2+dy**2 result math.sqrt(dsquared) return result #this part is good and provides needed stuff, the stuff you are about to see, gives me all sorts of problems. radius=distance(xc, yc, xp, yp) result=area(radius) return result #and this should be the last part def area2(xc, yc, xp, yp): radius=distance(xc, yc, xp, yp) result=area(radius) return result I should also mention that i'm a terrible mathematician, so there could potentially be some errors in the maths stuff itself. I appreciate any help.

30th Aug 2018, 12:28 PM
Nikola Radic
4 Respuestas
+ 7
For a more precise result you should import the constant pi from the math module. from math import pi def area_circle(radius): return pi * radius * radius
30th Aug 2018, 1:31 PM
Eduardo Petry
Eduardo Petry - avatar
+ 1
sorry but now I do not feel like reading your code hahah, a solution could be this: def area_circle(radius): return radius ** 2 * 3.14
30th Aug 2018, 1:01 PM
Ramphy Aquino Nova
Ramphy Aquino Nova - avatar
+ 1
Hm, ok, thank you for an answer, ill give it a try a bit later, since im not near my computer at this moment
30th Aug 2018, 1:05 PM
Nikola Radic
+ 1
Great, thanks for the input
30th Aug 2018, 1:33 PM
Nikola Radic