Checking the type of a variable for several types does not give the expected result

Task:

  1. Define a distance_from_zero function with one parameter.
  2. Let this function do the following:
    • Check the type of input it receives.
    • If the type is int or float , the function should return the absolute value of the input value of the function.
    • 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!" 
0
source share
7 answers

You cannot use such a "concatenation of logic based on natural language." I mean, you need to explicitly indicate parts of your logical conditions.

 if type(d) == int or type(d) == float 

So you have two comparisons that stand for themselves: if type(d) == int , and also type(d) == float . The results of this can be combined with the or operator.

+1
source

Type check should be

 if isinstance(d, int) or isinstance(d, float): 

which can be shortened

 if isinstance(d, (int, float)) 

What your current code is testing

 (type(d) == int) or float 

or, in words: "either type d is int , or float is true." For technical reasons, this whole expression is always true. Logical expressions in programming languages ​​should be specified a little more accurately than in a natural language.

+5
source

You should use isinstance here, not 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 will always be True , since it evaluates to float and this value is True :

 >>> bool(float) True 

help 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 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 object type in Python?

+4
source

In programming, if statements do not work, as in a simple 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 specifically for your problem, you want to use isinstance() instead of type() , since isinstance() will correctly consider subclasses. See this answer for more details.

So you should get 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!" 
0
source

Here is the 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! 

Pay attention to indentation, in Python it is very important in comparison with other languages.

0
source

Function Details:

 def distance_from_zero(d): if isinstance(d,(int,float)): return abs(d) else: return "Not an integer or float" 

Function call:

 print (distance_from_zero(5)) 

Output: 5

 print (distance_from_zero('5')) 

Conclusion: Not an integer or float

0
source

The mistake you make is to use too many English abbreviations.

 if type(d) == int or float: 

This means that you need to check whether the type int or float is True , which is not what you need.

 if type(d) == int or type(d) == float: 

This will give the desired result.

0
source

Source: https://habr.com/ru/post/1483847/


All Articles