You should use isinstance here, not type :
def distance_from_zero(d): if isinstance(d, (int, float)): return abs(d) else: return "Not an integer or float!"
if type(d) == int or float will always be True , since it evaluates to float and this value is True :
>>> bool(float) True
help isinstance :
>>> print isinstance.__doc__ isinstance(object, class-or-type-or-tuple) -> bool Return whether an object is an instance of a class or of a subclass thereof. With a type as second argument, return whether that is the object type. The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for isinstance(x, A) or isinstance(x, B) or ... (etc.).
Related: How to compare object type in Python?
source share