如何解决Typeerror int是不可调用的
我正在尝试编写一个程序,该程序使用一个函数根据用户输入的信息来计算简单兴趣。我收到TypeError-'int'无法调用。我以为该错误仅在您意外地将变量命名为int时发生,但我没有这样做,所以我不确定为什么我的程序中会出现这种类型的错误。下面的代码,任何指导表示赞赏!
def accrued(p,r,n):
percent = r/100
total = p(1 + (percent*n))
return total
principal = int(input('Enter the principal amount: '))
rate = float(input('Enter the anuual interest rate. Give it as a percentage: '))
num_years = int(input('Enter the number of years for the loan: '))
result = accrued(principal,rate,num_years)
print(result)
解决方法
更改总数= p *(1 +(百分比* n))
def accrued(p,r,n):
percent = r/100
total = p*(1 + (percent*n)) # * missing
return total
principal = int(input('Enter the principal amount: '))
rate = float(input('Enter the anuual interest rate. Give it as a percentage: '))
num_years = int(input('Enter the number of years for the loan: '))
result = accrued(principal,rate,num_years)
print(result)
,
更改:
total = p(1 + (percent*n))
收件人:
total = p*(1 + (percent*n))
没有*
,p(...)
被解析为函数调用。由于整数以p
的形式传递,因此会引起您所看到的错误。
您通过principal
从用户那里获得了int(input(...))
-这是一个整数。然后将其提供给您的功能:
result = accrued(principal,num_years)
作为第一个参数-您的函数将第一个参数作为p
。
然后您要做的
total = p(1 + (percent*n)) # this is a function call - p is an integer
这是您的错误来源:
TypeError-'int'不可调用
通过提供像*
这样的运算符来解决该问题
total = p*(1 + (percent*n))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。