I am trying to understand class and function and can't seem to figure out what is wrong with my code
<p>Calculating the area of a triangle</p>

<pre><code>class area:

    def traingle(self,height,length):
        self.height=height
        self.length=length

    def calculate(self,maths):
        self.maths= (self.height)*(self.length)*(0.5)

    def answer(self):
        print 'Hello, the aswer is %i'%self.maths

first= area()

first.traingle(4,5)

first.calculate

print first.answer
</code></pre>

<p>What about this?</p>

<pre><code>import math


class Triangle:

    def __init__(self, height, length):
        self.height = height
        self.length = length

    def calculate(self):
        return (self.height) * (self.length) * (0.5)

    def answer(self):
        print 'Hello, the aswer is %.2f' % self.calculate()

first = Triangle(4, 5)
first.answer()
</code></pre>

<p>Remember, to call a method you need to use parenthesis, when you're doing <code>first.answer</code> you're not executing your method, instead you should be doing <code>first.answer()</code></p>

<p>Another different solution for this type of problem could be something like this:</p>

<pre><code>import math


class Triangle:

    def __init__(self, height, length):
        self.height = height
        self.length = length

    def area(self):
        return (self.height) * (self.length) * (0.5)


class Quad:

    def __init__(self, width, height):
        self.width = width
        self.height = height

    def area(self):
        return self.width * self.height


for index, obj in enumerate([Triangle(4, 5), Quad(2, 3)]):
    print 'Area for {0} {1} = {2:.2f}'.format(obj.__class__, index, obj.area())
</code></pre>

<p>In any case, make sure you go through some of the available <a href="http://www.tutorialspoint.com/python/python_classes_objects.htm" rel="nofollow">python tutorials</a> out there in order to understand all the concepts first ;-)</p>

python oop