Built_in functions


#1



Can someone please explain me whats the problem with my code and how it should work?


def distance_from_zero(num):
    if int(num) or float(num):
        return abs(num)
        else:
            return "nope"
------------------------------------
  File "python", line 4
    else:
       ^
SyntaxError: invalid syntax


#2

an if/else statement should have the same indent level, since you want to execute else if if is false


#3

Hi, @roylavrov ,

In addition to correcting the indentation problem that @stetim94 identified, reconsider whether this test fulfills its purpose ...

if int(num) or float(num):

#4

def distance_from_zero(num):
if int(num) or float(num):
return abs(num)
else:
return "nope"


Oops, try again. Your function seems to fail on input 0 when it returned 'nope' instead of '0'

fixed it but now whats the problem?


#5

@appylpye is right, look at the instructions:
If the type of the argument is either int or float

i don't see you use type anwhere


#6

Thank you both for the help
this is the correct code I used:

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


#7

wouldn't improper spacing make it so the code doesn't work?


#8

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


#9

haha you copied your code right from the module


#10

as did i. either way an emphasizes can be made on making sure the code is correctly spaced


#11

you need markup to make indent show on the forum:

If you don't, the code might be indented correctly, but it won't show on the forum


#12

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