Mar-01-2017, 11:08 PM
Or you type check yourself:
Either of these strategies requires checking for exceptions further up in the program with try/except blocks. In general, you should only be using these strategies when handling input you don't control. If you control the input, you need to make sure the input is right.
def increment(a): if not isinstance(a, int): raise ValueError('Can only increment int.') return a + 1Another strategy is to force the int:
def increment(a): return int(a) + 1This will accept strings, but turn them into ints if possible. If it's not possible, python will handle raising the error for you.
Either of these strategies requires checking for exceptions further up in the program with try/except blocks. In general, you should only be using these strategies when handling input you don't control. If you control the input, you need to make sure the input is right.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures