1

My python function when called skips the if statement I have given as the condition and directly executes the else statement. I would like to know why I am running into this error. My code seeks to verify if by calling an argument of an int or float type, I can get an absolute value.

def dist_frm_zero(a):
    if type(a) == type(int) or type(a)== type(float)
        return abs(a)
    print(a)
    else:
        print('not possible')
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • 2
    you seem to have a few syntax errors there too – Matiiss Sep 11 '21 at 18:09
  • Functions shouldn't print anything on error. They should raise so it can be handled programmatically. – ggorlen Sep 11 '21 at 18:12
  • Does this answer your question? [Check if a number is int or float](https://stackoverflow.com/questions/4541155/check-if-a-number-is-int-or-float) – ggorlen Sep 11 '21 at 18:17
  • the shortest code to check for this is in [this](https://stackoverflow.com/a/69145551/14531062) answer (I am not the OP of it) – Matiiss Sep 11 '21 at 18:23

6 Answers6

2

This should be

def dist_frm_zero(a):
    if isinstance(a, (int, float))
        return abs(a)
    # having a print statement here breaks the if else flow
    else:
        print('not possible')
Alen Paul Varghese
  • 1,278
  • 14
  • 27
  • 1
    it is still incorrect, `isinstance` take only two arguments – Matiiss Sep 11 '21 at 18:14
  • learned something new today (argument as tuple for `isinstance` function), this is honestly the best answer so far – Matiiss Sep 11 '21 at 18:17
  • This aready exists with more information in [an answer with 119 upvotes in a thread with 588k views](https://stackoverflow.com/a/35487663/6243352). Can we not mark it as a dupe? – ggorlen Sep 11 '21 at 20:35
1

type(int) returns a <class 'type'>. You probably meant to check if the type(a) is an int or a float, not a type(int) or type(float). Additioanlly, note that the print(a) statement is illegal. If you really need it, indent it under the if and put it before the return statement.

def dist_frm_zero(a):
    if type(a) == int or type(a) == float
        return abs(a)
    else:
        print('not possible')
Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

It's much better to use isinstance like so:

def dist_frm_zero(a):
    if isinstance(a, int) or isinstance(a, float):
        return abs(a)
    else:
        print('not possible')
not_speshal
  • 22,093
  • 2
  • 15
  • 30
  • 1
    even better to use like in [this](https://stackoverflow.com/a/69145551/14531062) answer (TIL that you can pass tuple to `isinstance()` function) – Matiiss Sep 11 '21 at 18:20
0

Welcome to the python community! Your checks are not quite right, here you go -

def dist_frm_zero(a):
  if type(a) == int or type(a)== float:
    return abs(a)
  else:
    print('not possible')
mahoriR
  • 4,377
  • 3
  • 18
  • 27
0

You would rather need to check if a is an instance of either a float or an integer.

def dist_frm_zero(a):
    if isinstance(a, float) or isinstance(a, int):
        return abs(a)
    else:
        print('not possible')
ph140
  • 478
  • 3
  • 10
  • This is the same as [my](https://stackoverflow.com/a/69145560/9857631) answer. – not_speshal Sep 11 '21 at 18:13
  • @not_speshal not exactly exactly, but I agree, you were first too – Matiiss Sep 11 '21 at 18:15
  • i see, it was 15 seconds after. Is it the right policy to delete your own answer when you see someone else said the same seconds ago? – ph140 Sep 11 '21 at 18:16
  • No need to delete. See [here](https://meta.stackexchange.com/questions/1096/how-should-we-deal-with-duplicate-answers). Just letting you know it's a duplicate answer. – not_speshal Sep 11 '21 at 18:18
  • @ph140 deleting an your answer is not a policy, it depends on either if it is forcefully deleted/removed by voting or moderator or if you remove it voluntarily – Matiiss Sep 11 '21 at 18:18
  • ok, just wondered if I should delete it, since you pointed out it was the same – ph140 Sep 11 '21 at 18:19
0

Beside your syntax errors possible due to the copy paste, you need to check if the type of a is an int (float) not a type(int) (type float).

In python also types have a type: the type "type".

>>> a = 1
>>> type(a)
<class 'int'>

>>> type(int)
<class 'type'>

so your check should be:

if type(a) is int or type(a) is float:
    ...

Edit: In your comment you said it didn't work, so I put a snippet with the complete code so you can see it works and compare it to yours to see where you did different.

In [2]: def dist_frm_zero(a):
   ...:     if type(a) is int or type(a) is  float:
   ...:         return abs(a)
   ...:     else:
   ...:         print('not possible')
   ...:

In [3]: dist_frm_zero(10)
Out[3]: 10

In [4]: dist_frm_zero(-0.432)
Out[4]: 0.432

In [5]: dist_frm_zero("Ciao")
not possible

Another two things about your question.

  1. Fix the position of your print statement. In that position is a syntax error, and if intended to be in the if, it would still be after a return so it'll never be executed.

  2. A function that sometimes returns a number, sometimes returns None and prints a statement it's not the best from many points of view. A better solution would be to assert the input is correct or to raise a TypeError exception. Another solution would be to omit the checks and let the exception be thrown directly by the abs function which when an input with the wrong type is passed throws the following:

>>> abs("ciao")
TypeError: bad operand type for abs(): 'str'

So two better solutions would be:

def dist_frm_zero(a):
    return abs(a)


def dist_frm_zero(a):
    if type(a) not in [int, float]:
     raise TypeError("a must be an int or a float")
    return abs(a)
Gerardo Zinno
  • 1,518
  • 1
  • 13
  • 35