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)
添加回答
舉報