Alexander Craggs
Alexander Craggs

Reputation: 8819

Checking Multiple Integers in Python 3

Recently I found a method of testing whether or not a variable is an int or not in Python 3. It's syntax is like this:

try:
    a = int(a)
except ValueError:
    print("Watch out!  The Index is not a number :o (this probably won't break much in this version, might in later ones though!")
    error = True

However, when testing multiple variables it quickly becomes horrible to look at:

def IntegerChecker(a, b, c, d): #A is Index, B is End Time, C is Distance, D is Speed Limit
    global error
    try:
        a = int(a)
    except ValueError:
        print("Watch out!  The Index is not a number :o (this probably won't break much in this version, might in later ones though!")
        error = True
    try:
        b = int(b)
    except ValueError:
        print("Watch out!  The End Time is not a number :o")
        error = True
    try:
        c = int(c)
    except ValueError:
        print("Watch out!  The Distance is not a number :o")
        error = True
    try:
        d = int(d)
    except ValueError:
        print("Watch out!  The Speed Limit is not a number :o")
        error = True

Is there not an easier way to test whether a variable is an integer or not, and if it is not then change a variable to true and print a unique message to the user?

Please be aware that I am a more novice programmer in Python, however if there is a more complicated method to doing this which concise I would love to know. On another note, would this be better in the code review section of Stack Exchange?

Upvotes: 0

Views: 554

Answers (2)

Irshad Bhat
Irshad Bhat

Reputation: 8709

You don't need to have a separate try-except block for each variable. You can check all variables in one and if any of the variables is non-numeric exception will be raised and error thrown.

def IntegerChecker(a, b, c, d): #A is Index, B is End Time, C is Distance, D is Speed Limit
    global error
    try:
        a = int(a)
        b = int(b)
        c = int(c)
        d = int(d)
    except ValueError:
        print("Watch out!  The Index is not a number :o (this probably won't break much in this version, might in later ones though!")
        error = True

Upvotes: 1

yograterol
yograterol

Reputation: 603

This is my solution

def IntegerChecker(**kwargs): #A is Index, B is End Time, C is Distance, D is Speed Limit
    error = False
    err = {
        'index': ('Watch out!  The Index is not a number :o (this probably '
                  'won\'t break much in this version, might in later ones though!'),
        'end_time': 'Watch out!  The End Time is not a number :o',
        'distance': 'Watch out!  The Distance is not a number :o',
        'speed': 'Watch out!  The Speed Limit is not a number :o'
    }
    for key, value in kwargs.items():
        try:
            int(value)
        except ValueError:
            print(err.get(key))
            error = True
    return error

IntegerChecker(index=a, end_time=b, distance=c, speed=d)

Upvotes: 1

Related Questions