Jim
Jim

Reputation: 171

Python Class Runtime-Error

I have just started python and made a program about 8 line of code which only calculates the area of a triangle but when ever I try running it, I would get this error

File "", line 1, in t.Area(12,12) line 3, in Area length = self.num1 AttributeError: 'Triangle' object has no attribute 'num1'

And here is my code

class Triangle:
    def Area(self,num1,num2):
        length = self.num1
        width = self.num2
        area = (length * width) / 2
        area = int(area)
        print("Your area is: %s " %area)

help would be appreciated

Upvotes: 1

Views: 1060

Answers (2)

fuesika
fuesika

Reputation: 3330

As the message states: Your object does not feature an attribute num1 (and moreover no attribute num2). You need to set these somewhere in your class, i.e.

class Triangle:
    def __init__(self, num1, num2):
        #set length and width of triangle at instantiation
        self.length = num1
        self.width = num2

    def Area(self):
        #and all the other stuff here...

On the other hand, your method looks like you want to pass the two values num1 and num2. In that case, you just need to remove self. in front of the assumed attributes, since you are passing the values as arguments:

class Triangle:
    def Area(self,num1,num2):
        length = num1
        width = num2
        #and all the other stuff here...

Of course, you might as well cut num1 and num2 directly in this case:

class Triangle:
    def Area(self,length,width):
        #and all the other stuff here...

Upvotes: 1

shuttle87
shuttle87

Reputation: 15934

You need to make the variables be members of the class before you can use them via the self.num1 syntax. Before you specify self.num1 there is no num1 contained in your Triangle class. Specifically a class needs to have a way of initializing the members it contains. You can do this by creating a constructor with __init__ which gets called when you make an instance of Triangle. That approach would look something like this:

class Triangle:
    def __init__(self,num1,num2):
        self.width = num1 #now num1 is associated with your instance of Triangle
        self.length = num2

    def Area(self):#note: no need for num1 and num2 anymore
        area = (self.length * self.width) / 2
        print("Your area is: %s " % int(area))

Alternatively you can define those members within a different method (as in not __init__) like this

class Triangle:
    def SetDimensions(self,length,witdh):
        self.length = length
        self.width = width

    def Area(self):
        area = (self.length * self.width) / 2
        print("Your area is: %s " %int(area))

For more info about what self and __init__ do I'd recommend having a look at: Python __init__ and self what do they do?. If the Area method really has nothing to do with a particular instance and you just want to have a general purpose way of finding the area of a right angled triangle then a free function might be a better approach:

def RightAngleTriangleArea(self,length,width):
    area = (length * width) / 2
    area = int(area)
    print("Your area is: %s " %area)

Note that there's far better ways to actually specify a data type for a triangle if you do decide to go the route of a Triangle class.

Upvotes: 0

Related Questions