问题
Task:
- Define a function,
distance_from_zero
with one parameter. - Have that function do the following:
- Check the type of the input it receives.
- If the type is
int
orfloat
, the function should return the absolute value of the function input. - If the type is any other type, the function should return
"Not an integer or float!"
My answer that does not work:
def distance_from_zero(d):
if type(d) == int or float:
return abs(d)
else:
return "Not an integer or float!"
回答1:
You cannot use this kind of "natural language based logic concatenation". What I mean is that you need to state the parts of your logical conditions explicitly.
if type(d) == int or type(d) == float
This way you have the two comparisons, which stand for themselves: if type(d) == int
as well as type(d) == float
. The results of this can be combined with the or
-operator.
回答2:
The type check should be
if isinstance(d, int) or isinstance(d, float):
which can be abbreviated
if isinstance(d, (int, float))
What your current code is testing is
(type(d) == int) or float
or, in words: "either the type of d
is int
, or float
is true". For technical reasons, this entire expression is always true. Logical expressions in programming languages have to be specified a bit more precisely than in natural language.
回答3:
You should use isinstance
here rather than type
:
def distance_from_zero(d):
if isinstance(d, (int, float)):
return abs(d)
else:
return "Not an integer or float!"
if type(d) == int or float
is always going to be True
as it is evaluated as float
and it is a True
value:
>>> bool(float)
True
help on isinstance
:
>>> print isinstance.__doc__
isinstance(object, class-or-type-or-tuple) -> bool
Return whether an object is an instance of a class or of a subclass thereof.
With a type as second argument, return whether that is the object's type.
The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for
isinstance(x, A) or isinstance(x, B) or ... (etc.).
Related : How to compare type of an object in Python?
回答4:
Here is a correct code:
def distance_from_zero(d):
if type(d) in (int, float):
return abs(d)
else:
return "Not an integer or float!"
print distance_from_zero(3)
print distance_from_zero(-5.4)
print distance_from_zero("abc")
Output:
3
5.4
Not an integer or float!
Please be aware of indention, in Python is very important comparing to other languages.
回答5:
In programming, if statements don't work like they do in plain language. If you want to say something like This fruit is an apple or an orange
, you need to program it as
if type(fruit) == Apple or type(fruit) == Orange
More specific to your problem, you want to use isinstance() instead of type(), as isinstance()
will properly account for subclassing. See this answer for more details.
So you should end up with something like
def distance_from_zero(d):
if isinstance(d, int) or isinstance(d, float):
return abs(d)
else:
return "Not an integer or float!"
回答6:
The mistake you are making is using a little too much of English shortforms.
if type(d) == int or float:
This means check if the type is int
or if float
is True
, which is not what you want.
if type(d) == int or type(d) == float:
This will give required result.
来源:https://stackoverflow.com/questions/16838308/checking-type-of-variable-against-multiple-types-doesnt-produce-expected-result