Rectangular Python classes

I need to make a rectangle data class and an executable to test it.

* Here is my error message *

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

      

This is the complete problem I am dealing with:

In the main function being executed:

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.*

      

My question is why is my code not being executed completely. This is what it should look like at startup: I may have missed something too ...

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

      

If anyone wants to provide advice on the rest of it, do so. This is what I have for the rectangle class:

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

      

and this is my main file:

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

      

+3


source to share


1 answer


In your main file, you have:

rectangle = (length, width)

      

which doesn't create a new rectangle object. Do you want to



rectangle = rectangle.rectangle(length, width)

      

which is terse and will fail as well since you have local variable and imported module as the same name. You can fix this by using CamelCase

for your class names and simply importing the class:

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

...

    rectangle = Rectangle(length, width)

      

+2


source







All Articles