I need to check for floating point numbers inside my class but it doesn't seem to be working. Any suggestions?

``````class Rectangle:

def __init__(self, length = 1, width = 1):
self.__length = length
self.__width = width

##Setters to float
def set_length(self, length):
self.__length = float(length)
try:
#self.__length = float(length)
if self.__length > 0.0 and self.__length < 20.0:
print "Value is in range.  You may continute."
else:
raise ValueError
except ValueError:
print "Value must be between 0.0 and 20.0"

def set_width(self, width):
self.__width = float(width)
try:
#self.__width = float(width)
if self.__width > 0.0 and self.__width < 20.0:
print "Value is in range.  You may continute."
else:
raise ValueError
except ValueError:
print "Value must be between 0.0 and 20.0"
##Getters
def get_length(self):
return self.__length

def get_width(self):
return self.__width

##Get perimeter and area
def perimeter(self):
return 2 * self.__length + 2 * self.__width

def area(self):
return self.__length * self.__width``````
``````import rectclass

def main():
## Get length and width

print 'Enter numeric data greater than 0.0 but less than 20.0'
print
a_len = input('Enter Length: ')
print
wide = input('Enter Width: ')

calculations = rectclass.Rectangle(a_len, wide)

print calculations.perimeter()
print
print calculations.area()

main()``````
3
Contributors
3
Replies
5
Views
7 Years
Discussion Span
Last Post by Ghostenshell

What isn't working? It looks, at a glance, to be working.

Here it is. Now it works.

``````class Rectangle:

def __init__(self, length = 1, width = 1):
self.set_length(length)
self.set_width(width)

##Setters to float
def set_length(self, length):
length = float(length)
try:
if 0.0 < length < 20.0:
print "Value is in range.  You may continute."
self._length = length
else:
raise ValueError
except ValueError:
print "Value must be between 0.0 and 20.0"

def set_width(self, width):
width = float(width)
try:
if 0.0 < width < 20.0:
print "Value is in range.  You may continute."
self._width = width
else:
raise ValueError
except ValueError:
print "Value must be between 0.0 and 20.0"
##Getters
def get_length(self):
return self._length

def get_width(self):
return self._width

##Get perimeter and area
def perimeter(self):
return 2 * self._length + 2 * self._width

def area(self):
return self._length * self._width``````

Cheers and Happy coding

Thanks