Review: built-in Functions


def distance_from_zero(x):
if x == int or float:
return abs(x)
return "Nope"
print distance_from_zero(10)

this is my code but have no idea what I'v done wrong, help me please!


You can't just call a type, x is still '1' , and 'int' is recognized as < type 'int' >

There are two functions you can use to identify the type a variable has: type(x) which returns the type directly (int, float, str) or isinstance(x, [type]) which returns True or False

To use type, you'll have to create two dummy variables, lets say y and z
y = 1 (int type)
z = 1.0 (float type)

then your test would be
if type(x) == type(y) or type(z)
etc, etc

using isinstance([variable],[type]) returns True or False directly, so no dummy variables, you just feed it to the If statement, so

if isinstance(x,int) or isinstance(x,float) would work.

The problem with this one is that they play a dirty trick on you by passing the your variable a 'True' which translates to a '1' when evaluated as an integer, so you'll have to find a way to neutralize that.

I ended up writing another small function to delete values that are passed as True or False


I'm still not sure what I'v done wrong, can you please show me a code which would work and I'll figure out what I'v done wrong?


Okay never-mind I got it :smiley: Thanks for the help anyway!


Pay attentio to the following instruction:

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

You need to put the or function and you're not using it at the if (something) or (something):, that's why is keep failing.