Rera Rera - 1 month ago 8
Python Question

<__main__.Circle object at 0x05DE4250>

Can somebody help, please?

I am trying to make this program work

class Circle:
def __init__(self, radius):
self.__radius = radius;
if self.__radius <= 0:
raise ValueError('must not be less than or equal to 0')
elif not isinstance(self.__radius, (int, float)):
raise TypeError('must be an integer value')

def main():
try:
c = Circle("n")
except ValueError as x:
print("Error: " + str(x))
else:
print(c)

main()


The result should be

Error: Radius must be an integer value


but, instead, I am getting:

TypeError: '<=' not supported between instances of 'str' and 'int'


if I change the value of

c = Circle("n")


to

c = Circle(-10)


I will get the answer but if I change it to

c = Circle(10)


I will get this error

<__main__.Circle object at 0x05DE4250>

Answer Source

TypeError: '<=' not supported between instances of 'str' and 'int'

You need to change the order of your checks, so that it first checks if the type is correct before doing comparison.

class Circle:
    def __init__(self, radius):
        self.__radius = radius
        if not isinstance(self.__radius, (int, float)):
            raise TypeError('must be an integer value')
        if self.__radius <= 0:
            raise ValueError('must not be less than or equal to 0')

Now, as to this:

<__main__.Circle object at 0x05DE4250>

This is just the default representation of your object, you can customize this by overriding the __repr__ method in your class:

def __repr__(self):
   return 'Circle of radius: {}'.self(radius)