Python Forum
Why this python ValueError is not happening
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why this python ValueError is not happening
#1
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):
        pass
RectangleModule.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?")
Reply
#2
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...
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thank you i will take your advices to improve my code.

Big Grin
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Lightbulb What's happening here? jesse68 3 937 Feb-27-2023, 04:53 PM
Last Post: Gribouillis
  2 if statements happening at the same time M0TH 3 934 Nov-19-2022, 11:37 PM
Last Post: M0TH
  Python ValueError The value of a Series is abmbiguous nhl66pens 2 2,227 Oct-17-2020, 02:46 PM
Last Post: jefsummers
  Why is this happening? taner7 3 2,124 Oct-14-2020, 10:11 PM
Last Post: Skaperen
  Binding not explicitly declared happening karkas 4 2,857 Aug-05-2019, 05:09 PM
Last Post: karkas
  Python- Help with try: input() except ValueError: Loop code to start of sequence Aldi 2 6,291 Mar-08-2018, 03:46 AM
Last Post: Larz60+
  Something strange happening here !? TerryRitchie 4 3,892 Apr-20-2017, 07:14 PM
Last Post: nilamo
  Python Error (Traceback & ValueError) Help! helpplease 4 5,945 Dec-19-2016, 06:07 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020