Review: Built-In functions


#1




Your function seems to fail on input -10 when it returned <'type 'int>' instead of '10'


I expect the code to return abs


def distance_from_zero(num):
    if type(10): 
        return type(10)
        
    else:
        print "Nope"


#2

Instead of type(10), it needs to be abs(10.)


#3

the directions say "If the type of the argument is either int or float, the function should return the absolute value of the function input."

So you will have to use:
"if type(num) == int or type(num) == float:"

Then after that, you tell it what to do, which is to return the number's absolute value:
"return abs(num)"


#4

I did exactly what the instructions stated and it stills gives me an error message:

Oops, try again. Your function seems to fail on input True when it returned 'None' instead of 'Nope'

this is my code now
def distance_from_zero(num):
if type(num) == int or type(num) == float:
return abs(num)

else:
    print "Nope"

#5

I did exactly what the instructions stated and it stills gives me an error message:

Oops, try again. Your function seems to fail on input True when it returned 'None' instead of 'Nope'

this is my code now
def distance_from_zero(num):
if type(num) == int or type(num) == float:
return abs(num)

else:
print "Nope"


#6

The fact that one case returns a value, implies that the other case should, as well.

return "Nope"

The function was exited with no return value (print "..." is not returned) so Python responds with None. See the difference?


#7

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