简体   繁体   中英

int object not callable error

I'm trying this example:

在此输入图像描述

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)

.. but the error is:

TypeError: 'int' object is not callable

Is Python seeing p as a function? If so, is there no way I could do it without importing modules?

Thanks!

Change the line to

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

Python doesn't interpret variables that are next to each other as being multiplied (nor would it interpret n, t as that.

Assuming you are using 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)

Also double check the units for t , is it in months or years? The formula seems to suggest years (if n = 12 is months per year) but you are prompting for months.

If in python 2.x you would need to import division from __future__ , or convert r to float first. And you would use raw_input for the prompt.

You are trying to multiply by p, so you should be explicit and use a * :

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

Cast to float (thanks David R) to prevent int rounding problems in division.

  1. you need the "*" operator to multiply numbers
  2. dividing int by int does not give a float so cast one to a float multiply of divide by a float (you will see "*1." in some code to do this"
  3. your input line does not match the variables above (ie t should be years not months and n is number of times compounded per year ... 12 for monthly and 4 for quarterly etc)
  4. also need to change your 8 to .08 as a percentage

Try:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM