How to use abs


#1


File "python", line 4
return: abs(d)
^
SyntaxError: invalid syntax

It should return the absolute value or "nope" but it's giving me an error. and i can't seem to find anything wrong with the code. Help please?


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


#2

Hi again. I figured out my error. It's the fact that i added a colon (:slight_smile: after return


#3

however, can anyone tell me why the output is "None" for this code?
(i want it to give me the absolute value)

def distance_from_zero(d):
if d == int or float:
return abs(d)
else:
return "Nope"
distance_from_zero(-20)


#4

We need to test for both types in complete expressions:

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

The if statement may not be raising an exception, but it would appear that neither branch is followed so the function has no return value; hence, None.


#5

oh thank you very much


#6

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