Reputation: 39
def distance_from_zero(distance):
if type(distance) == int or type(distance) == float:
return abs(distance)
print(distance)
else:
return "Nope"
print distance_from_zero(distance)
distance_from_zero(18)
Upvotes: 0
Views: 228
Reputation: 4425
This is the original code with the print statements put in the correct place. Note the infinite recursion error that still shows up.
def distance_from_zero(distance):
if type(distance) == int or type(distance) == float:
print(distance)
return abs(distance)
else:
# This print forces infinite recursion because of distance
# print distance_from_zero(distance)
return "Nope"
This is a way of testing it
def distance_from_zero(distance):
if type(distance) == int or type(distance) == float:
print(distance)
return abs(distance)
else:
try:
distance = float(distance)
print(distance)
return abs(distance)
except:
print ("invalid distance specified", distance)
return "Nope"
Of course, the initial if can be dropped since float(int)
and float(float)
still return a normal float.
def distance_from_zero(distance):
try:
distance = float(distance)
print(distance)
return abs(distance)
except:
print ("invalid distance specified", distance)
return "Nope"
Upvotes: 0
Reputation: 65
I think that Python stops reading the definition after you've return
ed something, so you should swap the print
and return
commands around.
Upvotes: 0
Reputation: 87124
Firstly, why do you need to check the type of the argument before calling abs()
?. Just call it. abs()
knows best the types of operands that it can work with. If it sees one that it can't handle a TypeError
exception will be raised and the caller can deal with it.
If you really want to handle the error in your function, use a try/except
block instead of trying to guess the types that abs()
supports, e.g. complex numbers are supported; abs(complex(1, 2))
is perfectly valid:
try:
distance = abs(distance)
except TypeError as e:
print('{!r} is not a valid distance'.format(distance))
return None # or some other error
else:
print(distance)
return distance
Finally, if you really do want to limit support to only int
and float
types, it is better to use isinstance()
than type()
because is supports inheritance:
if isinstance(distance, (int, float)):
# it's a float or an int
distance = abs(distance)
A couple of other things to note:
print()
before calling return
in the function. return
terminates the function and returns execution to the calling code, so any statements after the return
will not be executed.You seem to want to print the value of distance after abs()
, so
you need to store the result in a variable (or perform the calculation twice):
distance = abs(distance)
print(distance)
return distance
Upvotes: 0
Reputation: 1968
The code after return will never be executed.
You have to revert the order.
Something like:
print(distance)
return abs(distance)
Upvotes: 0
Reputation: 5831
You should print before returning. Once you return, your print statement is not reachable.
replace:
return abs(distance)
print(distance)
with:
print(distance)
return abs(distance)
And this:
return "Nope"
print distance_from_zero(distance)
with:
print distance_from_zero(distance)
return "Nope"
Upvotes: 2