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))