0

Why am I getting the following error in this python code?

class polygon:
    def __init__(self, sides):
        self.sides = sides

    def display_info_polygon(self):
        print("it contains 5 sides")
    
    def perimeter_of_polygon(self):
        perimeter = sum(self.sides)
        return perimeter
    
class triangle(polygon):
    def display_info_triangle(self):
        print("Triangle has 3 sides")
    
class square(polygon):
    def display_info_square(self):
        print("square has 4 sides")
    
triangle1 = triangle()
triangle1.display_info_triangle()

This is the Traceback:

Traceback (most recent call last): File "C:\Users\CISPL-ABHINITESH\PycharmProjects\pythonProject\venv\perimeter_polygon_inheritance.py", line 20, in triangle1 = triangle() TypeError: init() missing 1 required positional argument: 'sides'

0

3 Answers 3

2

You are trying to run a class that takes one agr polygon but you're not giving it that value that's why it displays an error.

Your code:

triangle1 = triangle()
triangle1.display_info_triangle()

How it should be:

triangle1 = triangle(<YOUR POLYGON VALUE>)
triangle1.display_info_triangle()
Sign up to request clarification or add additional context in comments.

Comments

1

Read the Exception:

triangle1 = triangle() 
TypeError: __init__() missing 1 required positional argument: 'sides'

That means, when calling triangle(), the method __init__() of the parent class is called, which is missing one argument. So, the solution is to add that argument in the call:

triangle1 = triangle(3)

replace 3 with the value you want the argument sides to have.

Comments

0

triangle is inheriting polygon, so when you create an instance of triangle, the constructor of polygon is initiated, which takes an argument sides.

def __init__(self, sides):
      self.sides = sides

If you don't pass that argument, you will get an error. You should instantiate triangle by giving a sides value as an argument to the constructor like so:

triangle1 = triangle(7)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.