Help with Review: Built-In Functions


What's wrong?

def distance_from_zero(arg):
    return distance_from_zero 
    if  type (arg) == int or float:
        return abs(arg)
        return 'Nope'

Why does this happen?
Oops, try again. Your function seems to fail on input -10 when it returned '' instead of '10'


by default, a function returns none at the end

If you want to return something different, you can use the return keyword, this will cause the function to end


If I remove the first return though it says

Oops, try again. Your function seems to fail on input True when it returned '1' instead of 'Nope'
Why is this the case?


any idea what caused it? I helped you where the problem you where facing, in the hope you might be able to figure out any further issues.

When using or, comparison should be done at both sides of the or operator, if you don't do this, python will validate if float is true, while it should validate that type args equals float


in other words:
def distance_from_zero(x):
if type(x)== int:
return abs(x)
elif type(x)==float:
return abs(x)
return "Nope"


why would you do this? You can also just do comparison at both side of the or operator:

if True == True or False == False

see how i compare at both the left hand and right hand side of the or operator?


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