Manu S
Manu S

Reputation: 13

Checking type of variable against multiple types doesn't produce expected result

Task:

  1. Define a function, distance_from_zero with one parameter.
  2. Have that function do the following:
    • Check the type of the input it receives.
    • If the type is int or float, 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!"

Upvotes: 1

Views: 5141

Answers (6)

Polaris000
Polaris000

Reputation: 938

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.

Upvotes: 0

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250891

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?

Upvotes: 8

Mene
Mene

Reputation: 3799

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.

Upvotes: 1

thegrinner
thegrinner

Reputation: 12243

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!"

Upvotes: 0

Kostanos
Kostanos

Reputation: 10404

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.

Upvotes: 0

Fred Foo
Fred Foo

Reputation: 363527

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.

Upvotes: 7

Related Questions