整数对象不可调用错误

1 投票
4 回答
1520 浏览
提问于 2025-04-17 09:58

我正在尝试这个例子:

在这里输入图片描述

p = 10000
n = 12
r = 8
t = int(input("Enter the number of months the money will be compounded "))

a = p (1 + (r/n)) ** n,t

print (a)

.. 但是出现了这个错误:

TypeError: 'int' object is not callable

Python是不是把p当成了一个函数?如果是这样的话,有没有办法不导入模块就能做到呢?

谢谢!

4 个回答

-1

你想要进行乘法运算,所以要明确地使用一个*符号:

a = p * ((1 + (float(r)/n)) ** n,t)

将数字转换为浮点数(感谢David R的建议),这样可以避免在除法运算中出现整数四舍五入的问题。

5

把这一行改成

a = p * (1 + (r/n)) ** (n * t)

在Python中,紧挨着的变量不会被当作相乘来理解(也不会把n, t理解成相乘)。

1

假设你正在使用 Python 3。

p = 10000
n = 12
r = 8
t = int(input("Enter the number of months the money will be compounded: "))

a = p * (1 + (r / n)) ** (n * t)

print(a)

另外,检查一下 t 的单位,是以月为单位还是以年为单位?这个公式看起来是以年为单位的(如果 n = 12 是每年的月份),但你却在询问月份。

如果你使用的是 Python 2.x,你需要从 __future__ 导入除法,或者先把 r 转换成浮点数。而且你会用 raw_input 来获取输入。

撰写回答