Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to execute a class (python)

#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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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 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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading