-2

I have a script to create vlan in cisco switches, and I need the user to input a number to the script.

I'm having trouble to make the script validate if the input is a number(must be int) and if the number is inside the range 2~4094, before it moves to the next steps.

I already wrote the rest of the script.

  • Try `str.isdigit()`. For example, `"1".isdigit()` returns `True` and `"1.1".isdigit()` returns `False`, then you can check its range. – Tianbo Ji May 27 '19 at 15:46
  • @TianboJi, `isdigit("½")` will return `True`. – Olvin Roght May 27 '19 at 15:50
  • @OlvinRoght What is your Python version? I tested `"½".isdigit()` on Python 3.7.3 and 2.7.10, both return `False`. – Tianbo Ji May 27 '19 at 15:57
  • @TianboJi, sorry, this example is for `isnumeric()`. `'³'.isdigit()` this will return True for both python versions. The idea is for validation before type cast to int `isdecimal()` should be used. – Olvin Roght May 27 '19 at 16:03
  • @OlvinRoght you are correct, `'³'.isdigit()` does return `True`. `isdecimal()` is a better solution for this question. – Tianbo Ji May 27 '19 at 16:06

4 Answers4

3

This should work:

def validate(num):
    try:
        n = int(num)
        return 2 <= n <= 4094
    except:
        return False

The above function returns True if the number is an integer in the specified range, False otherwise.

Óscar López
  • 225,348
  • 35
  • 301
  • 374
  • thanks... but when I try you example... if the user input something like TESTE besides an int, the scripts goes on. – Douglas May 27 '19 at 15:59
  • We'd need to know how you're calling your script, and how you're using my function. You need to provide a lot more details for people to be able to help you... – Óscar López May 27 '19 at 16:02
0
try:
    val = int(user_input)
    print("user input is an int")
except ValueError:
    print("user_input is not an int")
vinzee
  • 17,022
  • 14
  • 42
  • 60
0

You can use str.isdecimal() to check if string contains only digits.

inp = input("Enter number: ")
if inp.isdecimal():
    n = int(inp)
else:
    print("Not a number")

If you need function:

def validate(inp):
    return isinstance(inp, str) and inp.isdecimal()


inp = input("Enter number: ")
print(validate(inp))
Olvin Roght
  • 6,675
  • 2
  • 14
  • 32
0

When the function below return True, you can do the Math you need to do with the number.

def as_int_in_range(value,range):
    try:
        value = int(value)
        if value >= range[0] and value <= range[1]:
            return True,value 
        else:
            return False,None 
    except ValueError:
        return False,None

is_int,val = as_int_in_range('12',(10,100))
if is_int:
    # do something with 'val' assuming it is int in the range
else:
    # do something  else        
balderman
  • 21,028
  • 6
  • 30
  • 43
  • thank you... but if the input is not a int, I need the script to ask again for the value, until the provided input is a int – Douglas May 27 '19 at 15:57
  • @Douglas, apply `while` loop. Check [this](https://stackoverflow.com/a/56256888/10824407) answer, I've provided some examples there. – Olvin Roght May 27 '19 at 16:06