Polygon Area Calculator (project #4)

Hi all,

I’m working on the next project at FCC and got stuck on probably very basic error.

Error:

python main.py
True
True
Rectangle(width=3, height=10)
Traceback (most recent call last):
File “main.py”, line 12, in
sq = shape_calculator.Square(9)
File “/home/runner/boilerplate-polygon-area-calculator/shape_calculator.py”, line 74, in init
super().init(height , width)
NameError: name ‘height’ is not defined
exit status 1

My code is:

class Rectangle():
    
    def __init__(self, width, height):
        self.height = height
        self.width = width 
        self.area = True
        self.diagonal = True
        self.perimeter = True
  
#set_width, set_height, get_area: Returns area (width * height)      
    
    def set_width(self, width):
        self.width = width
        
    def set_height(self,height):
        self.height = height

    def set_area(self, area):
        self.area = self.width * self.height
        
    def get_area(self):
        return self.area
    
    #get_perimeter: Returns perimeter (2 * width + 2 * height)
    def set_perimeter(self, perimeter):
        self.perimeter = 2 * width + 2 * height
        
    def get_perimeter(self):
        return self.perimeter
  
    #get_diagonal: Returns diagonal ((width ** 2 + height ** 2) ** .5)
    def set_diagonal(self, diagonal):
        self.diagonal = (width ** 2 + height ** 2) ** .5
        
    def get_diagonal(self):
        return self.diagonal
 
    #get_picture: Returns a string that represents the shape using lines of "*". 
    #The number of lines should be equal to the height width.
    #There should be a new line (\n) at the end of each line. 
    #If the width or height is larger than 50, 
    #this should return the string: "Too big for picture.".
    
    def set_picture(self, picture):
        if self.width > 50 or self.height > 50:
            print("Too big for picture.")
        else:
            
      #found on google python print pattern 
            for i in range(self.width):
                for k in range(self.height):
                    print("*", end = " ")
                print()
             
    def get_picture(self):
        
        return self.picture

   
    #get_amount_inside: Takes another shape (square or rectangle) as an argument. Returns the number of times the passed in shape could fit inside the shape (with no rotations). For instance, a rectangle with a width of 4 and a height of 8 could fit in two squares with sides of 4.

    def set_amount_inside():
        pass
    def get_amount_inside(self):
        pass
    
#Additionally, if an instance of a Rectangle is represented
#as a string, it should look like: 
#Rectangle(width=5, height=10)
    def __str__(self):
        return f'Rectangle(width={self.width}, height={self.height})'

#the Square class should be a subclass of Rectangle. 
class Square(Rectangle):
    def __init__(self, side):
        super().__init__(height , width)
        self.side = side
        self.height = side
        self.width = side
    
    #also contain a set_side method.      
    def set_side(self, side):

        self.height = side
        self.width = side   
        
    def get_side(self):
        return self.width or self.height
        
    def __str__(self):
        return f'Square(side={self.side})'
    

I tried to work around since yesterday but run out of options.

Error is originating from here:

class Square(Rectangle):
    def __init__(self, side):
        super().__init__(height , width)
        self.side = side
        self.height = side
        self.width = side

To be specific line that’s calling the __init__ method of parent class.

super().__init__(height , width)
NameError: name ‘height’ is not defined
1 Like

Thank you, I realised, I didn’t fully understand these init and super init in OOP so spent weekend revising fundamentals and now it works.

1 Like

The main problem of the above code is

super().init(height , width)

height and width variable is not defined in Square class.

super().init(12 ,14)
you need to pass some values inside this constructor then this line will call its parent class constructor to initialize values inside variables.

always remember use self keyword while you use global(class) variables.
removed error in this code.

def set_perimeter(self, perimeter):
self.perimeter = 2 * self.width + 2 * self.height

def get_perimeter(self):
    return self.perimeter

#get_diagonal: Returns diagonal ((width ** 2 + height ** 2) ** .5)
def set_diagonal(self, diagonal):
    self.diagonal = (self.width ** 2 + self.height ** 2) ** .5

[advertising link removed]

When the last post in a topic concludes with “now it works” and hasn’t had activity for a prolonged period of time (like a whole month) - please don’t necro it.