亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

類型錯誤 int 不可調用

類型錯誤 int 不可調用

largeQ 2023-07-18 10:14:15
我正在嘗試編寫一個程序,該程序使用一個函數根據用戶輸入的信息計算單利。我收到一個類型錯誤 - 'int' 不可調用。我認為這個錯誤只發生在你意外命名變量 int 時,但我沒有這樣做,所以我不確定為什么我的程序中會出現這種類型的錯誤。代碼如下,感謝任何指導!def accrued(p, r, n):    percent = r/100    total = p(1 + (percent*n))    return totalprincipal = 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)
查看完整描述

3 回答

?
慕桂英3389331

TA貢獻2036條經驗 獲得超8個贊

改變:

total = p(1 + (percent*n))

到:

total = p*(1 + (percent*n))

如果沒有*,p(...)則被解析為函數調用。由于整數被作為 傳遞p,因此它導致了您所看到的錯誤。


查看完整回答
反對 回復 2023-07-18
?
海綿寶寶撒

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


查看完整回答
反對 回復 2023-07-18
?
翻翻過去那場雪

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)


查看完整回答
反對 回復 2023-07-18
  • 3 回答
  • 0 關注
  • 194 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號