Home > database >  TypeError: 'int' object is not callable with function
TypeError: 'int' object is not callable with function

Time:01-10

New to coding and haven't encountered this type of return ant keep getting this error. I can only edit the 'f =' variable.

def a_plus_abs_b(a, b):
    """Return a abs(b), but without calling abs.
    
    >>> a_plus_abs_b(2, 3)
    5
    >>> a_plus_abs_b(2, -3)
    5
    """
    if b < 0:
        f = (-1)*b   a
    else:
        f = (a b)
    return f(a, b)

CodePudding user response:

You can directly return f in your function because it already holds the result of your calculation.

As for the TypeError, it just indicates that int can not be called like that, if not for type convert(e.g. int(-3.5)).

The corrected version:

def a_plus_abs_b(a, b):
    """Return a abs(b), but without calling abs.
    
    >>> a_plus_abs_b(2, 3)
    5
    >>> a_plus_abs_b(2, -3)
    5
    """
    if b < 0:
        f = (-1)*b   a
    else:
        f = (a b)
    return f

CodePudding user response:

You can just immediately return the result of the addition instead of using f=. Since you are storing the result of the addition to the variable f, the line return f(a,b) results in an error because f is of the type int.

Example:

if b < 0:
    return (-1)*b   a

  • Related