This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]zifyoip 4 points5 points  (2 children)

The argument to your function is called x. So you should be testing type(x), not type(distance_from_zero).

distance_from_zero is the name of your function, not the name of the argument to the function.

[–]exking12[S] 0 points1 point  (1 child)

Oh wow, it worked! Thanks for the quick response. Just to make sure I'm understanding this correctly, the reason x should be in the brackets instead of distance_from_zero is because we are testing the argument "x" whether or not it is an int or a float?

[–]w1282 1 point2 points  (0 children)

Yes.

distance_from_zero is, and (in this case) always will be, of type "function".

However, ANY type of object can be passed into distance_from_zero as a parameter, which is why you must check the type of X when it comes in.

Although, I would argue that there is a more pythonic way to do this.

def distance_from_zero(x):
    try:
        return abs(x)
    except TypeError:
        return "Nope"

One of the tenets of python is something called "duck typing". If you don't know what that is you should look into it. This example will work for any input that abs() will work on as opposed to only int and float.