Review built in functions help


#1

My code is this:
def distance_from_zero(j):
if j == type(j) == int or type(j) == float:
return abs(j)
else:
return "Nope"

i've changed the code many times but i keep getting an error saying: Oops, try again. Your function seems to fail on input -10 when it returned 'Nope' instead of '10'
and the cosole window just returns "none"


#2

@jdgarc17

In your code you put
... if j == type(j) == int or type(j) == float:

Remove the j == part and all should be well.!


#3

it gives the same message


#4

@jdgarc17
Here's a reviewed version of your code, try it and see if it works:

 `def distance_from_zero(j):
if type(j) == int or type(j) == float:
    return abs(j)
else:
    return "Nope"`

#5

Okay i see what you meant/what i did wrong this worked thank you..


#6

I'm getting the same error so I tried this code and it didn't work, not sure what's going on.


#7

Here is my code:

What is wrong???

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


#8

I'm also getting an error on this step. Pretty much the same code as posted and getting the error;
"Your function seems to fail on input True when it returned 'None' instead of 'Nope'".
Looking for any help to get past this step.

edit : Please disregard this post. I solved the problem but not quite sure what I did. I think it may have something to do with indentation, though I was not getting an indentation error.


#9

try this one

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


#10

you have to put the colon after the if like this

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


#11

@jdgarc17 @bitrockstar16077 @steve305 @wizmarco You need to maintain indentation in this code :smile:


#12

Thanks.
I did following your advices, even though I used the
else:
return "Nope"

Solution.
how would you have written using elif syntax, compairing with the type() function?