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

这一生的挚爱 提交于 2021-02-11 15:12:33

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!