19/19 simple question!


#1

This is what I type below

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

And I got error message saying "Oops, try again. Your function seems to fail on input True when it returned '1' instead of 'Nope'"

I looked at other people's answer and the difference between correct answer and mine are in line 2, as the correct answer is

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

So I now know the correct answer but I was wondering how is my answer (if type(arg) == int or float: ) interpreted as in Python? I just don't understand why can't my answer work...


#2

You have to do it like

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

It has to be able to iterate over it, if it can't it returns the first thing it compares to.


#3

Thanks for the fast reply! you really help me clear things up! :smile:


#4

sorry I did it your way and it gave me an error message.


#5

It should not be doing that, give me a min


#6

Do it like this

type(9.5) in (int, float)

Although the `or one should work...

Let me do a little research.


#7

Take a gander at this,

for x in range(0, 11):
    for y in range(11):
        if (x or y) % 2 == 0:
            print(x, y)

See the statement

if (x or y) % 2 == 0:

It works the way it should, so I don't get why it is goofing up with they type comparison.

When you put in

type(9.5) == (int or float)

You get False which it should not do. I wonder...

type(int) == int

This gets me a False as well, well I think I found the issue.

type(9.5) == (type(int) or type(float))

No, that was not it.

I would just stick with,

This is most likely the best way to check for typing.