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()
您好,我我是一名Python編程初學者。這是我對象和類的第一個程序在訪問類文件時遇到問題 – user2607610