I defined the following function to test if an input is a positive int. I plan on using eval(raw_input("...")) so that's why the try-except part is there:
def is_ok(x): # checks if x is a positive integer
is_int = False
is_pos = False
try:
if type(eval(x)) == int:
is_int = True
if x > 0:
is_pos = True
else:
pass
else:
pass
except NameError:
is_int = False
print "not even a number!"
return is_int, is_pos
if I try to pass a positive int it would return True, True as expected. It returns False, False and the error message for a non-number.
However, for negative numbers it still returns True for checking if it's positive. For example, adding this after the function :
is_int, is_pos = is_ok("-9")
print is_int, is_pos
running prints: True True
Can't understand why that is and would love your help. Even if there are more efficient ways to accomplish this, I'd still like to understand why it produces True True. Thanks!
(This is somewhat of a followup to: Python: How do I assign 2 values I return from a function with 1 input as values outside the function?)