I've attempted this in two different ways, but both approaches raise the TypeError when I attempt to assign a value to length or width during my testing of it. I've tried both 5, and 5.0, and somehow, neither are integers or floats.
Here is my code, with both approaches for __setattr__ in it, one being commented out:
class Rectangle:
def __init__(self):
self.__width = 1
self.__length = 1
def __setattr__(self,name,value):
if name == "__width":
if ((value is float) or (value is int)) and (0.0<=value<=20.0):
self.__width = value
elif ((0.0<=value<=20.0) == False):
raise ValueError
elif ((value is float) or (value is int)) == False:
raise TypeError
else:
raise Exception("An unexpected error has occured. Please try again.")
if name == "__length":
if ((value is float) or (value is int)) and (0.0<=value<=20.0):
self.__length = value
elif ((0.0<=value<=20.0) == False):
raise ValueError
elif ((value is float) or (value is int)) == False:
raise TypeError
else:
raise Exception("An unexpected error has occured. Please try again.")
## def __setattr__(self,name,value):
## if name == "__width":
## if type(value) != type(float) and (type(value) != type(int)):
## raise TypeError('Must be float or integer')
## elif 0.0 <= value <= 20.0:
## raise ValueError('Must be between 0 and 20')
## else:
## self.__width = value
## elif name == "__length":
## if (type(value) != type(float)) and (type(value) != type(int)):
## raise TypeError('Must be float or integer')
## elif 0.0 <= value <= 20.0:
## raise ValueError("Must be between 0 and 20")
## else:
## self.__length = value
## else:
## self.__dict__[name] = value
def __getattr__(self, name):
if name == "__width":
return self.__width
elif name == "__length":
return self.__length
else:
raise AttributeError("Specified attribute not found or defined.")
def calcPerim(self):
return "The perimeter is",((2*self.__width)+(2*self.__length)),"units"
def calcArea(self):
return "The area is",(self.__width*self.__length),"square units"
x = Rectangle()
x.__length = 5.0
x.__width = 7
x.calcPerim()
x.calcArea()
The error I get is:
Traceback (most recent call last):
File "C:\Python27\RectangleClass.py", line 60, in <module>
x.__length = 5.0
File "C:\Python27\RectangleClass.py", line 23, in __setattr__
raise TypeError
TypeError
I feel like the issue is somewhere in my if/elif/else statements logic more than likely, but I'm mediocre with debugging, at best, so I have no idea.
Any input on a better approach, or towards a solution with my current code would be greatly appreciated.
For clarification, here is the problem specification I must follow:
Exercise 2 (25 points)
Write a class Rectangle that has attributes __length and __width, each of which defaults
to 1. It also has methods that calculate the perimeter and the area of the rectangle. It has
set and get methods for both __length and __width. The set methods should verify hat
__length and __width are each floating-point numbers larger than 0.0 and less than 20.0.
the method should raise special exceptions if the numbers are not in the correct range
(between 0 and 20) or the used input is not of type float.
Write a small program to test the class.

New Topic/Question
Reply




MultiQuote




|