19.Review: Built-in functions: Why my code is broken?


#1

It shows the error "Oops, try again. Your function seems to fail on input True when it returned '1' instead of 'Nope' "

I don't get it. Why is it broken?

def distance_from_zero(n):
    if type(n) != str:
        return abs(n)
    else:
        return "Nope"

distance_from_zero(-8)


#2

This line:

    if type(n) != str:

Should be:

    if type(n) == int or type(n) == float:

#3

Why? Isn't it the same? If the type is not a string it should be an int or a float. Or there are more types?


#4

Yes, there are a lot more. They are just not covered in the introductory courses. Here is more information on the subject:
https://docs.python.org/2/library/stdtypes.html


#5

Thank you so much. Really. =)
I'll read it all.


#6

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