r/pythonhelp • u/sonnylunadogs • Jun 12 '22
Trying to check if a value is a positive integer, what am I doing wrong? Getting 'True' for all values. Thank you!!!
def isPosInt(val):
if type(val) == int:
return True
else:
if int(val) >= 0:
return True
else:
return False
isPosInt(-5)
3
Upvotes
3
u/MrHyderion Jun 12 '22
Look at what your function does.
It checks whether the variable passed into it is an int -> if yes, it returns
true-> if no, it checks whether the variable is >= 0, returningtrueif yes, andfalseif no.So obviously each time you pass an int to this function, it will return
trueright away without ever checking if it's positive.