19/19 - SyntaxError


#1

This is my code:

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

And I keep getting this error message..

File "python", line 2
if type(thing) == int or type(thing)
^
SyntaxError: invalid syntax

What am I doing wrong?


#2

I would personally not type check like that it just leads to issues as you can see. Also I would put in error handling for any input that is not valid as follows.

Do the Following:

def distance_from_zero(item):
    try:
        return abs(item)
    except TypeError as error:
        print("You have had an error: %s" % error)

#3

The problem is the evaluation in if:
if type(thing)==int(thing) or type(thing)==float(thing)

you cant compare only with int or float