以前發佈過關於錯誤的問題。感謝這裏的幾個人,我能夠解決這個問題。現在我遇到了我的複利計算器的問題,當您輸入本金,複利利息(年,月等等),利率(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等於用戶輸入的數字,這會產生正確的最終答案。
在此先感謝。
爲什麼應該是1000.10?如果我理解正確,年複利率爲0.01,那麼1000 *(1.01)^ 1 = 1010.0。我在這裏錯過了什麼嗎? –
當我通過複利計算器運行數字三重檢查答案時,它給了我1000.10。 –
添加了答案。請檢查這是否是你想要的。 –