Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to execute a class (python)

Tags:

python

oop

class

#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

like image 206
Mitchel Avatar asked Nov 16 '25 09:11

Mitchel


2 Answers

  • 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
like image 189
Abhi Avatar answered Nov 18 '25 23:11

Abhi


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
like image 30
Artyom Vancyan Avatar answered Nov 18 '25 21:11

Artyom Vancyan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!