![]() |
Why this python ValueError is not happening - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Why this python ValueError is not happening (/thread-12329.html) |
Why this python ValueError is not happening - aurquiel - Aug-20-2018 Hello I have been trying to construct a class to check the atributes of an object, the error must raise if one of the atribute of the object is zero or negative, the zero case is working good but is not the case of the negative case Files of the procject Main.py #!/usr/bin/python3.6 from RectangleModule import Rectangle if __name__ == '__main__': myRectanle = Rectangle(10,-20) myRectanle.area() myRectanle.printArea()This is just an abstract class, every shape has a area and printArea method. ShapeModule.py from abc import ABCMeta, abstractmethod class Shape(metaclass=ABCMeta): @abstractmethod def area(self): pass @abstractmethod def printArea(self): passRectangleModule.py from ShapeModule import Shape from ShapeErrorModule import ShapeErrorsAtributes class Rectangle(Shape): def __init__(self, parameterRectangleWidth, parameterRectangleHeigh): self.rectangleWidth = parameterRectangleWidth self.rectangleHeigh = parameterRectangleHeigh ShapeErrorsAtributes(parameterRectangleWidth, parameterRectangleHeigh) super(Rectangle,self).__init__() def area(self): self.rectangleArea = self.rectangleWidth * self.rectangleHeigh def printArea(self): print(self.rectangleArea)And here is the code for the negative atribute case that does not run, why not? ShapeErrorModule.py class ShapeErrorsAtributes(Exception): def __init__(self,*parameterShapeAtributes): self.shapeAtributes = parameterShapeAtributes self.__shapeAtributeZero() self.__shapeAtributeNegative() def __shapeAtributeZero(self): for atribute in self.shapeAtributes: if atribute == 0: ValueError("ERROR: One atribute of the shape is Zero\n") def __shapeAtributeNegative(self): for atribute in self.shapeAtributes: if atribute < 0: ValueError("ERROR: One atribute of the shape is Negative\n") else: print("I din't raise the error, Why not?") RE: Why this python ValueError is not happening - buran - Aug-20-2018 you need to raise it, e.g. if atribute == 0: raise ValueError("ERROR: One atribute of the shape is Zero\n")I don't see how it could work for attribute==0 case. Also it's better to have one function that validate the input, not two identical with only error message different... RE: Why this python ValueError is not happening - aurquiel - Aug-20-2018 Thank you i will take your advices to improve my code. ![]() |