1

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

Jim
  • 171
  • 8
  • 4
    I see you have some answers on C++. Think of `self` as Python's `this`, only that instead of being implicit it's always explicitly there. So, in your case, it makes no sense to write `self.num1`, as `num1` is an argument to the function – Ricardo Cárdenes Feb 18 '14 at 01:26
  • 2
    I don't understand what you are trying to do. In your own words, why have you created a `Triangle` class, instead of just writing the function by itself? – Karl Knechtel Feb 18 '14 at 01:53

2 Answers2

1

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...
fuesika
  • 3,280
  • 6
  • 26
  • 34
0

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.

Community
  • 1
  • 1
shuttle87
  • 15,466
  • 11
  • 77
  • 106
  • There is no point in passing `num1` and `num2` to the method `Area` in your code example. These values would not be used. – fuesika Feb 18 '14 at 01:27
  • And if you are going to introduce `__init__` you may as well model the triangle more sensibly ... `__init__(self, sideA, sideB, angleAB)` or whatnot – wim Feb 18 '14 at 01:28
  • Why would one want to assign values to attributes within a method of a class that is supposed to handle a different task? This is not an example of well-arranged code. – fuesika Feb 18 '14 at 01:32
  • 1
    @pyStarter: I just tried to address the syntax issue initially but as I answered I realized the design needed a lot of attention. – shuttle87 Feb 18 '14 at 01:42
  • @wim, sure there's far better ways of modelling a triangle, I'm more than aware of that. I just wanted to address the python-specific error message in the original question. – shuttle87 Feb 18 '14 at 01:43