19. review built in functions im confused


#1




i keep getting this message and i'm really confused.
Oops, try again. Your function seems to fail on input True when it returned '1' instead of 'Nope


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


#2

We need to test hola for both types using the same function:

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

#3

The hint says his way should work


#4

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


#5

#6

Just saw this comment so went ahead and checked. In Python3 IDLE,

>>> type(3.5) == int or float
<class 'float'>
>>>

Python is treating True as 1 in the type check, so returns 1. Should have taken a closer look.

>>> isinstance(True, int)
True
>>>

>>> def distance_from_zero(hola):
    if type (hola) == int or float and not isinstance(hola, bool):
        return abs(hola)
    else:
        return "Nope"

>>> distance_from_zero(True)
'Nope'
>>>

#7

def distance_from_zero(w):
if type(w) == int:
return abs(w)
elif type (w) == float:
return abs(q)
else:
return "Nope"


#8

What is wrong here? I really cant understand///


#9

q is not defined, but w is. Check the Hint for an example.


#11