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


#1



https://www.codecademy.com/en/courses/python-beginner-c7VZg/4/3?curriculum_id=4f89dab3d788890003000096#


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)
    else:
        return "Nope"


#2

The error should be here:

You should instead do:

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

#3

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


#4

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)


#5

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


#6

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
Arun


#7

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.


#8

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


#9

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)
    else:
        return "Nope"

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

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

#10

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


#11

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

So
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.


#12

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)


#13

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.


#14

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