How to use abs


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)
        return: "Nope"


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


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)
return "Nope"


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.


oh thank you very much


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