19. Review: Built in Functions problem


#1

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

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


#2

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)
    else:
        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.


#3

thank you so much, it worked!


#4

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