Help with Review: Built-In Functions


#1

What's wrong?

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

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


#2

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


#3

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?


#4

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


#5

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


#6

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?


#7

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