3 回答

TA贡献2036条经验 获得超8个赞
改变:
total = p(1 + (percent*n))
到:
total = p*(1 + (percent*n))
如果没有*
,p(...)
则被解析为函数调用。由于整数被作为 传递p
,因此它导致了您所看到的错误。

TA贡献1809条经验 获得超8个赞
您可以principal
通过 - 从用户处获取int(input(...))
- 所以它是一个整数。然后你将它提供给你的函数:
result = accrued(principal, rate, num_years)
作为第一个参数 - 您的函数将第一个参数作为p
。
然后你做
total = p(1 + (percent*n)) # this is a function call - p is an integer
这就是你的错误的根源:
类型错误-“int”不可调用
通过提供像这样的运算符来修复它*
total = p*(1 + (percent*n))

TA贡献2065条经验 获得超14个赞
变化总计 = 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)
添加回答
举报