writing python functions incorporating if and absolute

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')

6 answers

  • answered 2021-09-11 18:10 Mureinik

    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')
    

  • answered 2021-09-11 18:10 Alen Paul Varghese

    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')
    

  • answered 2021-09-11 18:10 mahoriR

    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')
    

  • answered 2021-09-11 18:11 not_speshal

    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')
    

  • answered 2021-09-11 18:11 ph140

    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')
    

  • answered 2021-09-11 18:17 Gerardo Zinno

    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)
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum