0

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?

Alexander Craggs
  • 7,874
  • 4
  • 24
  • 46
  • Lazy answer: don't bother testing. If converting to an int causes an unhandled crash, the user is getting what they deserve by putting in nonsense input. – Kevin Nov 04 '14 at 16:48
  • Alternatively, [Asking the user for input until they give a valid response](http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) may be useful to you. – Kevin Nov 04 '14 at 16:49
  • Fair enough =) However it is necessary for all variables to be tested. @Kevin The data is not inputted by a human but by a computer. However the computer sometimes decides not to send me an integer (I do not have access to this computer or it's code). Oh, and whoever downvoted, please say why. I'm relatively new to this site, having only asked/answered 20 questions. – Alexander Craggs Nov 04 '14 at 16:49

2 Answers2

1

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)
yograterol
  • 593
  • 3
  • 7
1

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
Irshad Bhat
  • 8,479
  • 1
  • 26
  • 36