Home > Blockchain >  How to Improve this Class to Except for Negative Ints or Floats?
How to Improve this Class to Except for Negative Ints or Floats?

Time:10-12

I have the following method, which I use an an exception for any inputs I take from the user.

    def enter_data(self, message: str, typ: type):
        while True:
            try:
                v = typ(input(message))
            except ValueError:
                print(f"Thats not an {typ}!")
                continue
            else:
                break
        return v

With this method, I can specify the value type I am looking for, as well as keep the user in this loop until they enter that type.

Such as this:

 def add_item_interaction(self) -> None:
   add_item_num = self.enter_data("What is the new items #?\n", int)
   add_item_price = self.enter_data("What is the new items price?\n", float)
   add_item_quant = self.enter_data("What is the new items quantity?\n", int)
   add_name = self.enter_data("What is the new items name?\n", str)

Unfortunately, this still allows a user to enter a negative int or float. I have tried to edit it as so to account for this, but these do not work:

    def enter_data(self, message: str, typ: type):
        while True:
            try:
                v = typ(input(message))
                if v < 0:
                    raise ValueError
            except ValueError:
                print(f"Thats not an {typ}! or you have entered a negative")
                continue
            else:
                break
        return v

This one does not allow the string data type, as you obviously cannot compare a string to a less than.

I also tried something such as:

    def enter_data(self, message: str, typ: type):
        while True:
            try:
                v = typ(input(message))
                if isinstance(v, int) or isinstance(v, float) and v < 0:
                    raise ValueError
            except ValueError:
                print(f"Thats not an {typ}! or you have entered a negative")
                continue
            else:
                break
        return v

This doesn't seem to work for any type entered, no matter if it is a negative or matches what I specify.

Any Help Here? I know I must be close.

For example:

When I run add_item_num = self.enter_data("What is the new items #?\n", int), and input 1, I get: "Thats not an int!", even though the input is an int, and positive, so it should not trigger the if isinstance () statement

CodePudding user response:

This boils down to a True or False and False = True problem. Without paranthesis this is executed like this

or(True, and(False,False))

The first value is instantly returned and the others are not evaluated.

Your idea is perfect you just need to add the parantheses

   def enter_data(self, message: str, typ: type):
        while True:
            try:
                v = typ(input(message))
                if (isinstance(v, int) or isinstance(v, float)) and v < 0:
                    raise ValueError
            except ValueError:
                print(f"Thats not an {typ}! or you have entered a negative")
                continue
            else:
                break
        return v
  • Related