paintballlslag
paintballlslag

Reputation: 1

Why do I keep getting: 'int' object is not callable in Python?

x = 4
y = 5
a = 3(x + y)
print(a)

I keep trying to solve that. I've even tried this.

x = input("Enter value for x:")

Enter value for x:4

y = input("Enter value for y:")

Enter value for y:5

a = 3 x + y

What am I doing wrong?

Upvotes: 0

Views: 74

Answers (2)

lc.
lc.

Reputation: 116538

To multiply, you need to use the * operator:

a = 3 * (x + y)

3(x + y) looks and feels like a function call for the function 3, which is why you're getting the error.

FWIW, algebraic expressions where you simply abut two symbols next to each other does not translate directly in most programming languages, unless they were specifically designed for mathematics.

Upvotes: 2

Jon Hanna
Jon Hanna

Reputation: 113392

I suspect that you want 3(x + y) to be acted upon as that would be in algebra, that is to multiply the result of x + y by 3. For that you need to use the multiplication operator *:

a = 3 * (x + y)

Python will take the parentheses after a token as a function call as per f(x + y), and since 3 is not a function, you are told it is not "callable" (meaning a function, or something you can treat as a function).

Upvotes: 2

Related Questions