EthanMcQ
EthanMcQ

Reputation: 31

TypeError: 'int' object is not callable with function

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)

Upvotes: 0

Views: 326

Answers (2)

Elbert Wang
Elbert Wang

Reputation: 33

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

Upvotes: 1

Leon
Leon

Reputation: 31

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

Upvotes: 0

Related Questions