Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (50.2k points)

I want to develop a code that should calculate the area of a circle. I am passing the radius of the circle as an argument to the function and the equation to calculate the area is PI*r2

area = PI*r2

def SetArea (myradius, myarea):

    PI = 3.14159

    myarea = PI*myradius *2

    return myarea

When I execute the code, the error is 'PI is not defined'... Kindly guide.

1 Answer

0 votes
by (108k points)

First of all, you need to delete your 1st line of code as in that line, you have simply mentioned the PI variable, but you have not defined that variable, so that is why you are getting this kind of error message. 

Your function code is fine, but in your code, you need to calculate the area of the circle so for that the formula is Pi*R*R, so it will be ** 2, not * 2. You can also use the math package:

import math

def calculate_area(radius):

    return math.pi * radius ** 2

If you are newbie and want to explore more about Python, then learn python from the below video tutorial:

Related questions

0 votes
0 answers
0 votes
0 answers
0 votes
1 answer

Browse Categories