Home > Mobile >  Why is my program not displaying correctly?
Why is my program not displaying correctly?

Time:12-02

The requirements for this problem are:

■ Two data fields named width and height.

■ A constructor that creates a rectangle with the specified width and height.

The default values are 1 and 2 for the width and height, respectively.

■ A method named getArea() that returns the area of this rectangle.

■ A method named getPerimeter() that returns the perimeter.

Write a test program that creates two Rectangle objects—one with the width 4 and height 40 and the other with width 3.5 and height 35.7. Display the width, height, area, and perimeter of each rectangle in this order.

I do not need to draw a UML diagram.

class rectangle():
    def __init__(self,w=4,h=40):
        self.width = width
        self.height = height 
    def compute_area(self):
        return (self.height * self.width)
    def compute_perimeter(self):
        return 2 * (self.height   self.width)
def main_1():
    print("Rectangle width is,",self.width)
    print("Rectangle height is,",self.height)
    print("Rectangle area is,",compute_area)
    print("Rectangle perimeter is,",compute_perimeter)

class rectangle_2():
    def __init__(self,w=3.5,h=35.7):
        self.width = width
        self.height = height
    def compute_area(self):
        return (self.height * self.width)
    def compute_perimeter(self):
        return 2 * (self.height   self.width)

def main_2():
    print("Rectangle 2 width is,",self.width)
    print("Rectangle 2 height is,",self.height)
    print("Rectangle 2 area is,",compute_area)
    print("Rectangle 2 perimeter is,",compute_perimeter)

main_1()
main_2()

When I run the program, it states:

Traceback (most recent call last):

File "C:/Users//OneDrive/Desktop/Programming/7.1.py", line 30, in main_1()

File "C:/Users//OneDrive/Desktop/Programming/7.1.py", line 10, in main_1

print("Rectangle width is,",self.width)

NameError: name 'self' is not defined

I've tried seeing how other people have solved this problem but, they all use 'self' without defining it as Python wants and the program works. I'm not sure what to do.

CodePudding user response:

Check this code, since it is not clear what you are trying to do

class rectangle():
    def __init__(self,width=4,height=40):
        self.width = width
        self.height = height 
    def compute_area(self):
        return (self.height * self.width)
    def compute_perimeter(self):
        return 2 * (self.height   self.width)
    def main_1(self):
        print("Rectangle width is,",self.width)
        print("Rectangle height is,",self.height)
        print("Rectangle area is,",self.compute_area())
        print("Rectangle perimeter is,",self.compute_perimeter())

class rectangle_2():
    def __init__(self,width=3.5,height=35.7):
        self.width = width
        self.height = height
    def compute_area(self):
        return (self.height * self.width)
    def compute_perimeter(self):
        return 2 * (self.height   self.width)

    def main_2(self):
        print("Rectangle 2 width is,",self.width)
        print("Rectangle 2 height is,",self.height)
        print("Rectangle 2 area is,",self.compute_area())
        print("Rectangle 2 perimeter is,",self.compute_perimeter())

rectange = rectangle()
rectange.main_1()
rectange_2 = rectangle_2()
rectange_2.main_2()
  • Related