0
votes

In Python I have been trying out classes for the first time. When I use this code I get the error 'This constructed does not take arguments' on line 15. Can someone please tell me what the problem is?

class Triangle:
    def _init_(self,h,b):
        self.h = h
        self.b = b
    author = 'No one has claimed this rectangle yet'
    description = 'None'
    def area(self):
        return (self.h * self.b)/2
    def description(self,text):
        self.description = text
    def author(self,text):
        self.author = text

fred = Triangle(4,5)
print fred.area()
3
init with double underscores on both sides, not just one : __init__, and not _init_ - pixis

3 Answers

0
votes

Your error is in the init function. It is supposed to have two underscores before and after like this __init__().

Here is the correct code:

class Triangle:
    def __init__(self,h,b):
        self.h = h
        self.b = b
    author = 'No one has claimed this rectangle yet'
    description = 'None'
    def area(self):
        return (self.h * self.b)/2
    def description(self,text):
        self.description = text
    def author(self,text):
        self.author = text

fred = Triangle(4,5)
print fred.area()
2
votes

You should use double underscores __ to denote __init__:

def __init__(self, h, b):
2
votes

You have defined your constructor as _init_ when it should be defined as __init__ (note the double underscore). Python is not seeing your __init__ (as it is misnamed), and is just assuming a default constructor (which does not take arguments).