矩形类Python

矩形类Python,python,Python,我需要制作一个矩形数据类和一个可执行程序来测试它 *这是我的错误消息* >>> ================================ RESTART ================================ >>> Length? 7 (then next line) Width? 8 (then next line) Traceback (most recent call last): File "/Users/AngelaDon1/D

我需要制作一个矩形数据类和一个可执行程序来测试它

*这是我的错误消息*

>>> ================================ RESTART ================================
>>>  Length? 7 (then next line) Width? 8 (then next line)
Traceback (most recent call last):
File "/Users/AngelaDon1/Desktop/Don_Asst4/Assg4_Don.py", line 26, in <module>
main()
File "/Users/AngelaDon1/Desktop/Don_Asst4/Assg4_Don.py", line 16, in main
print(rectangle.get_length(), rectangle.get_width())
# print the length & width
AttributeError: 'tuple' object has no attribute 'get_length'
>>> 
我的问题是,为什么我的代码没有一直执行。 下面是跑步时的样子:也许我也错过了一些东西

SAMPLE OUTPUT
Enter the length 12.45
Enter the width 8.973
Length: 12.45
Width: 8.973
Rectangle area is 111.71
Rectangle perimeter is 42.85
Changed rectangle area is 352.81
Changed rectangle perimeter is 76.27
如果有人想提供其余部分的提示,请提供。这是我为矩形类准备的:

class rectangle:

    def __init__(self, length, width): # this initializes the object with the given parameters

        self.__length = length # assign length

        self.__width = width # assign width

    def set_length(self, length): # this method allows us pass the Rectangle object a value and set the object's length to the given value

        self.__length = length # assign length

    def set_width(self, width): # had 'model' here # same thing, for width

        self.__width = width # assign width

    def get_length(self): 

        return self.__length 

    def get_width(self):

        return self.__width

    def get_area(self): # multiples w x h

        return self.get_width() * self.get_length() 

    def get_perimeter(self): 

        return self.get_width() * 2 + self.get_length() * 2 
这是我的主要文件:

import rectangle

def main(): 

    length = float(input('Length? ')) # turn length string to float

    width = float(input('Width? ')) # turn width string into float

    rectangle = (length, width) 

    print(rectangle.get_length(), rectangle.get_width()) # print the length &     width

    print(round(rectangle.get_area(), 2)) 

    print(round(rectangle.get_perimeter(), 2)) 

    print(round(rectangle.get_area(), 2))

    print(round(rectangle.get_perimeter(), 2)) 

main()

在主文件中,您有:

rectangle = (length, width)
它不会生成新的矩形对象。你想要的是

rectangle = rectangle.rectangle(length, width)
这有点冗长,而且也会失败,因为您有一个局部变量,并且导入的模块具有相同的名称。您可以通过对类名使用
CamelCase
,然后只导入类来解决此问题:

from rectangle import Rectangle  # change the name in rectangle.py as well

...

    rectangle = Rectangle(length, width)

它正在打印/做什么…?>>>=======================================================================================================>>>长度?7(然后是下一行)宽度?8(然后是下一行)回溯(最后一次调用):文件“/Users/AngelaDon1/Desktop/Don_Asst4/Assg4_Don.py”,第26行,在main()文件“/Users/AngelaDon1/Desktop/Don_Asst4/Assg4_Don.py”,第16行,在主打印中(矩形.get_length(),矩形.get_width())#打印长度和宽度属性error:“tuple”对象没有属性“get_length”>>>>您可能应该编辑您的问题以包括(这是有用的信息)将变量设置为与导入相同的名称可能也是一个坏主意。我收到的错误消息与开始时相同。在长度之后?宽度呢?提示我收到此错误:回溯(最后一次调用):文件“/Users/AngelaDon1/Desktop/Don_Asst4/Assg4_Don.py”,第26行,在main()文件“/Users/AngelaDon1/Desktop/Don_Asst4/Assg4_Don.py”中,第14行,在主矩形=矩形(长度、宽度)UnboundLocalError:在赋值之前引用了局部变量“矩形”。非常感谢Ethan!
from rectangle import Rectangle  # change the name in rectangle.py as well

...

    rectangle = Rectangle(length, width)