19.Review: Built-In Functions


I already done this section correctly but i have a question about it.
The right anser is:

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

But i wonder could i write the "if" line as:
if type(num) == int or float:

I tried to pass this section but it didn't accept that code.


because then python simply check if float is true, which it is:

if float:
    print True

so then your if condition is always true

for us, this a normal shorthand, for the computer its not


def distance_from_zero(n):

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


My intention is to see the result after i specify the value. I am unsuccessful in printing the results. whenever there is if statement involved i am unable to print. I dont know what i am missing. help is appreciated


a function ends the moment a return keyword is reached

so your print statement is never reached

you can add a print keyword before the function call to print what the function returns


Explanation about that thing in my way is that:
1) if only check condition which pass
so that__
if we use that code
if type(num)== int or float # always true that execute return statement
but in second condition which is a right to handle the code is:
if type(num) == int # if bool then it is false or type(num) == float:#if it is bool then resukt is false
so false or false = false
then it execute the else statement
return abs


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