运行程序时出现错误
Enter the length of the rectangle: 4
Enter the width of the rectangle: 2
Traceback (most recent call last):
File "C:\Users\Shourav\Desktop\rectangle_startfile.py", line 50, in <module>
main()
File "C:\Users\Shourav\Desktop\rectangle_startfile.py", line 34, in main
my_rect = Rectangle()
TypeError: __init__() missing 2 required positional arguments: 'length' and 'width'
代码:
# class definition
class Rectangle:
def __init__(self, length, width):
self.__length = length
self.__width = width
self.__area = area
def set_length(self, length):
self.__length = length
def set_width(self, width):
self.__width = width
def get_length(self, length):
return self.__length
def get_width(self, width):
return self.__width
def get_area(self, length, width):
area = (length * width)
self.__area = area
return self.__area
# main function
def main():
length = int(input("Enter the length of the rectangle: "))
width = int(input("Enter the width of the rectangle: "))
my_rect = Rectangle()
my_rect.set_length(length)
my_rect.set_width(width)
print('The length is',my_rect.get_length())
print('The width is', my_rect.get_width())
print('The area is',my_rect.get_area())
print(my_rect)
input('press enter to continue')
# Call the main function
main()