int对象不可调用错误

时间:2012-01-11 05:24:58

标签: python

我正在尝试这个例子:

enter image description here

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 个答案:

答案 0 :(得分:5)

将行更改为

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

Python不会将彼此相邻的变量解释为相乘(也不会将n, t解释为。

答案 1 :(得分: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作为提示。

答案 2 :(得分:-1)

您正在尝试乘以p,因此您应该明确并使用*

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

施放浮动(感谢David R)以防止划分中的int rounding问题。

答案 3 :(得分:-1)

  1. 您需要使用“*”运算符来乘以数字
  2. 将int除以int不给浮点数,因此将一个浮点数乘以浮点数除以浮点数(在某些代码中你会看到“* 1。”这样做“
  3. 您的输入行与上述变量不匹配(即t应该是年而不是月份,n是每年复合的次数...每月12次,季度4次等)
  4. 还需要将您的8更改为.08作为百分比
  5. 尝试:

    p * (1 + (r/100.0/n)) ** (n * t)