Reputation: 33
I need to make a rectangle data class and an executable program 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 full problem that I am dealing with:
In the executable's main function:
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 executing all of the way. Here is what is should look like when run: Maybe i'm missing 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 tips on the rest of it, please do. 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'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
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()
Upvotes: 3
Views: 3331
Reputation: 69051
In your main file you have:
rectangle = (length, width)
which does not make a new rectangle object. What you want is
rectangle = rectangle.rectangle(length, width)
which is a bit wordy, and will also fail as you have a local variable and the imported module as the same name. You can fix that by using CamelCase
for your class names, and just importing the class:
from rectangle import Rectangle # change the name in rectangle.py as well
...
rectangle = Rectangle(length, width)
Upvotes: 2