19.Review: Built-In Functions


#1

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)
    
    else:
        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.


#2

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


#3

def distance_from_zero(n):

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

distance_from_zero(-10)

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


#5

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


#6

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


#7

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