class Area(object):
def __init__(self, base, height):
self.base = base
self.height = height
def calculation(self):
return(self.base * self.height)
area = Area(15, 2)
print(Area.calculation())
Asked
Active
Viewed 72 times
-2

TheEagle
- 5,808
- 3
- 11
- 39

The noob coder
- 15
- 1
-
Change your last line to print(Area.calculation(area)) – randomer64 Oct 09 '21 at 14:02
2 Answers
0
The Area
in your print()
is capitalized, while your variable area
is not. That means that you are calling the Area Object instead of the variable.
This should work :
class Area(object):
def __init__(self, base, height):
self.base = base
self.height = height
def calculation(self):
return(self.base * self.height)
area = Area(15, 2)
print(area.calculation())

Nathn
- 418
- 5
- 17
0
You are calling the class object's calculation
method (Area.calculation
), instead of calling the calculation
method of the Area
instance (area.calculation
). This works:
class Area(object):
def __init__(self, base, height):
self.base = base
self.height = height
def calculation(self):
return self.base * self.height
area = Area(15, 2)
print(area.calculation())
Note the removed parentheses around the return
argument - they are unnecessary as return
is a keyword, not a function.

TheEagle
- 5,808
- 3
- 11
- 39