问题
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?
回答1:
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).
回答2:
To multiply, you need to use the *
operator:
a = 3 * (x + y)
Or,
a = 3 * x + y
depending on which one is correct in your case.
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.
来源:https://stackoverflow.com/questions/27239898/why-do-i-keep-getting-int-object-is-not-callable-in-python