Mar-05-2019, 06:39 PM
(Mar-05-2019, 05:38 PM)ClassicalSoul Wrote: What alternative would you recommend?
You can use isdigit(), but that will be False for '-1', which will convert to an integer. If you only want positive numbers, that's not a problem. If you want to include negatives, the conditional becomes a little messy:
if text.isdigit() or (text[0] == '-' and text[1:].isdigit()):
. At that point I would just go with try except:try: print(int(x) + 2) except ValueError: if x.lower() == 'quit': break else: print('Integers only, please.')
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