3 回答
TA貢獻(xiàn)2036條經(jīng)驗(yàn) 獲得超8個(gè)贊
改變:
total = p(1 + (percent*n))
到:
total = p*(1 + (percent*n))
如果沒有*,p(...)則被解析為函數(shù)調(diào)用。由于整數(shù)被作為 傳遞p,因此它導(dǎo)致了您所看到的錯(cuò)誤。
TA貢獻(xiàn)1809條經(jīng)驗(yàn) 獲得超8個(gè)贊
您可以principal通過 - 從用戶處獲取int(input(...))- 所以它是一個(gè)整數(shù)。然后你將它提供給你的函數(shù):
result = accrued(principal, rate, num_years)
作為第一個(gè)參數(shù) - 您的函數(shù)將第一個(gè)參數(shù)作為p。
然后你做
total = p(1 + (percent*n)) # this is a function call - p is an integer
這就是你的錯(cuò)誤的根源:
類型錯(cuò)誤-“int”不可調(diào)用
通過提供像這樣的運(yùn)算符來修復(fù)它*
total = p*(1 + (percent*n))
TA貢獻(xiàn)2065條經(jīng)驗(yàn) 獲得超14個(gè)贊
變化總計(jì) = 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)
添加回答
舉報(bào)
