Rectangle Classes Python

时间:2015-07-03 00:29:25

标签: python

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

*这是我的错误消息*

>>> ================================ 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'
>>> 

这是我正在处理的完整问题:

在可执行文件的主要功能中:

prompt the user to enter the length and width of a rectangle.

create a new Rectangle instance with the dimensions entered by the user.

to verify the above step, print both dimensions using their respective "getter" methods.

test the area() method by printing the rectangle area, accurate to two 
decimal places.

test the perimeter() method by printing the rectangle perimeter, accurate to  
two decimal places.

change the length to 22.345 and change the width to 15.789.

test the area() and perimeter() methods again. You should get the results       
shown in the sample output.*

我的问题是,为什么我的代码不会一直执行。 这是运行时应该是什么样的:也许我也错过了一些东西......

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()

1 个答案:

答案 0 :(得分:2)

在您的主文件中,您有:

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)