#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
c1 = Circle()
c1.setRadius(7)
c1.getArea()
print("c1.Area", c1.area)
c1.Area 153.86
c1.getArea() only, then below change in the class code would help for c1.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
calcArea and calcCircumference should return some values as those are used in other methods to calculate the results. Also, the radius is a property and not callable.
Change the class definition like below
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):
self.calcCircumference()
Then you will get the following results
c1 = Circle()
print("c1.Area", c1.getArea()) # c1.Area 0.0
c1.setRadius(7)
print("c1.Area", c1.getArea()) # c1.Area 153.86
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With