這是我寫的代碼,我沒能得到預期的結果。 我總是得到一個重復了8次的金額。
amount = float(input('the principal amount: '/span>))
rate = float(input('年收益率:')
時間 = int(input('how many years it will take: ')
def invest(amount, rate, time)。
總額=金額*(1 速率)**時間
print(total)
for t in range(time)。
total_new = amount * (1 rate)**time
print(total_new)
投資(金額, 費率, 時間)
預期收益 for invest(100, 0.05, 8) 。
Year1: $105: $105.
第二年:$110.25。
第3年:$115.7625147.745544379。
uj5u.com熱心網友回復:
你的函式是正確的,問題只在于你的for回圈。 它應該是這樣的:
for t in range(time)。
total_new = amount * (1 rate) ** t
print(total_new)
你的錯誤是把所有的東西都設定為時間的冪,而不是t的冪。變數t在變化,而時間保持不變,所以你得到了8次相同的結果。
除此之外,我還建議在(1 rate) ** t周圍使用小括號,以明確它發生在乘以金額之前。
uj5u.com熱心網友回復:
你需要改變回圈中的變數,將t代替time:
for t in range(1,time)。
total_new = amount * (1 rate)**t
print(total_new)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/327349.html
標籤:
