#I am making a program that will calculate area and perimeter or the circle. No errors but the answer is not what I expect it to be.
class Circle:
def __init__ (self):
self.radius = 0
def setRadius(self,radius):
self.radius = radius
def calcArea (self):
self.area = 3.14 * (self.radius ** 2)
def calcCircumference (self):
self.circumference = 2 * (3.14 * self.radius )
def getRadius (self):
return self.radius ()
def getArea (self):
return self.calcArea ()
def getCircumference (self):
self.calcCircumference ()
#My execution
c1 = Circle ()
print("c1.Area",c1.getArea())
c1.setRadius(7)
print("c1.Area",c1.getArea())
#this is the answer I get when I execute it.
c1.Area None
c1.Area None
>Solution :
- Based on your class definition below is the code how you will get the area printed.
c1 = Circle()
c1.setRadius(7)
c1.getArea()
print("c1.Area", c1.area)
c1.Area 153.86
- Incase you want it to be returned by calling
c1.getArea()only, then below change in the class code would help forc1.getArea() and c1.getCircumference().
class Circle:
def __init__ (self):
self.radius = 0
def setRadius(self,radius):
self.radius = radius
def calcArea (self):
self.area = 3.14 * (self.radius ** 2)
return self.area
def calcCircumference(self):
self.circumference = 2 * (3.14 * self.radius )
return self.circumference
def getRadius (self):
return self.radius()
def getArea (self):
return self.calcArea()
def getCircumference (self):
return self.calcCircumference()
c1 = Circle ()
c1.setRadius(7)
print("c1.Area", c1.getArea())
print("c1.Area", c1.getCircumference())
# Below is the output
c1.Area 153.86
c1.Circumference 43.96