19. Review: Built in Functions problem


def distance_from_zero(n):
if type(n) == ("int"):
return abs(n)
elif type(n) == ("float"):
return abs(n)
return ("Nope")

error says that -10 doesnt return 10 and i dont know why. Please could someone help.


For one, since you are doing the same thing to the number if it's int OR float you can say:

def distance_from_zero(i):
    if type(i) == int or type(i) == float:
        return abs(i)
        return "Nope"

The second issue is you're checking to see if a type is equal to a string. As you see above, types are not "string literals". "float" is not float.


thank you so much, it worked!


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.