Functions 19. Review: Built-In Functions *solved*


I get the error message "Oops, try again. Your function seems to fail on input True when it returned '1' instead of 'Nope'" but 1 and True are the same so I don't see how this can possibly be fixed.

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


The error should be here:

You should instead do:

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


Thank you it worked, why exactly does that make a difference though?


it makes a huge difference, if you don't compare at both sides, it is just going to check if float is true, which is true as you can see:

if float:
   print True

float is considered true, so your if statement will always be true. Even if the numbers is not a integer or a float, simply because float is always true (and you are not comparing)


The statement/expression you wrote doesn't check to see if n is of float type.


Hi there

I am sorry I still do not understand, can you please explain it in a different way please?

I still cannot see the difference between if type(n) == int or type(n) == float: and if type(nums) == int or float:

Many thanks


if we have this:

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

you are comparing if type of n is either a float or a integer. However if you have this:

if type(nums) == int or float:

on the left side, you check if nums is of type integer. But on the right side of your or operator, you are not comparing, so python will simply check if float (since you don't compare it) is true or false. It literately check if the keyword float is considered true, which if you run this code:

if float:
    print True

you can see that the float keyword is considered true

so this:

if type(nums) == int or float:

will always be true, since the float keyword is considered true.


Excellent, it all makes sense now.Thank you. I just did not consider the two sides of the OR operator.


Hi all,

I tried something a little different when typing the code for this and wasn't sure why it didn't work. It tells me that it doesn't work for the float -9.5 although I can't see why it wouldn't...

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

I changed the code to have two comparative statements in the end i.e.

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


this doesn't work for any floating number, which makes me suspect it first evaluates this one:

(int or float)

and then it int is considered true (and is first), so it uses int. That is what i think


I don't quite follow... doesn't what is in the bracket get evaluated first?

if type(n) == (int or float)
is only true if n is both an integer and a float (impossible), if n is an integer or if n is a float.


just like with math, you do the thing between brackets first. so somehow python is evaluating this:

int or float

int is considered true, and then it is compared with integer or somethiing. I don't understand this either, i just know it doesn't work like that (how some things work under the hood are really dificult)


Sort of similar to the OP except what is being evaluated is within brackets... hmm, I think I follow now.

Either way the code only works if comparisons are explicitly stated.

I thought by doing it my way, the code would be neater and more concise. Clearly not.

If anyone else can expand, that would be great.


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