2016-01-22 128 views
0

以前發佈過關於錯誤的問題。感謝這裏的幾個人,我能夠解決這個問題。現在我遇到了我的複利計算器的問題,當您輸入本金,複利利息(年,月等等),利率(0.03等等)以及金額年份。 其他Q鏈接:final = P * (((1 + (r/n)) ** (n*t))) TypeError: unsupported operand type(s) for /: 'str' and 'int' 因此,從前面的代碼中,我刪除了p = 10000,n = 12,r = 0.08,因爲當您輸入不同的數字時,它會給出一個很大的數字。我的希望是它會用輸入的數字來計算它,但它不會。如何讓Python複合利息計算器給出正確的答案

# User must input principal, Compound rate, annual rate, and years. 
P = int(input("Enter starting principle please. ")) 
n = int(input("Enter Compound intrest rate.(daily, monthly, quarterly, half-year, yearly) ")) 
r = float(input("Enter annual interest amount. (decimal) ")) 
t = int(input("Enter the amount of years. ")) 

final = P * (((1 + (r/n)) ** (n*t))) 
#displays the final amount after number of years. 
print ("The final amount after", t, "years is", final) 
# At the moment it is displaying a very random number. 

Enter starting principle please. 1000 
Enter Compound intrest rate.(daily, monthly, quarterly, half-year, yearly) 1 
Enter annual interest amount. (decimal) 0.01 
Enter the amount of years. 1 
The final amount after 1 years is 1010.0 

最終的金額應該是1000.10。不知道發生了什麼事。試着看看是否有辦法讓P,n,r等於用戶輸入的數字,這會產生正確的最終答案。

在此先感謝。

+0

爲什麼應該是1000.10?如果我理解正確,年複利率爲0.01,那麼1000 *(1.01)^ 1 = 1010.0。我在這裏錯過了什麼嗎? –

+0

當我通過複利計算器運行數字三重檢查答案時,它給了我1000.10。 –

+0

添加了答案。請檢查這是否是你想要的。 –

回答

0

如果您輸入百分比感興趣,您應該在代碼中注意這一點。

final = P * (((1 + (r/(100.0 * n))) ** (n*t))) 
+0

啊。甚至沒有想到這一點。謝謝,我現在就運行它並讓你知道。 –

+0

它的工作原理!謝謝,自6點以來一直在做這些項目,我不敢相信我錯過了這些。感謝@ Sagar –

+0

歡迎。如果解決了您的問題,請接受答案。 :P –

相關問題