我需要编写一个代码来显示 0 到 20 年的投资回报,或者当今天的价值达到投资初始投资金额的 0.5 * 时。我被后半部分困住了。当我使用 IF 语句时,我无法获得任何价值。这是我的代码。
def getExpInvestAmt(investAmt, i, n):
expInvestAmt = int(investAmt * pow(1 + i / 100, n))
return expInvestAmt
def getPresentValue(exp_invest_amt, d, n):
adjAmt = int(exp_invest_amt / pow(1 + d / 100, n))
return adjAmt
def main():
investAmt = float(input("Enter investment amount:"))
i = float(input("Enter the expected investment growth rate:"))
d = float(input("Enter the discount rate:"))
n = 0
adjA = 0
print("Year \t Value at year($) \t Today's worth($)")
for x in range(0, 21):
if adjA < 0.5 * investAmt
break
expected_value = getExpInvestAmt(investAmt, i, n)
present_value = getPresentValue(expected_value, d, n)
n += 1
adjA += 1
print("{} \t {:,} \t {:,}".format(x, expected_value, present_value))
main()
这就是我想得到的
Enter investment amount: 10000
Enter the expected investment growth rate: 5
Enter the discount rate: 6.25
Year Value at Year($) Today's worth($)
1 10,500 9,346
2 11,025 8,734
3 11,576 8,163
4 12,155 7,629
5 12,763 7,130
6 13,401 6,663
7 14,071 6,227
8 14,775 5,820
9 15,513 5,439
10 16,289 5,083
11 17,103 4,751 # today's value have reached <= 50% of the initial amount program stops.
我认为你可以简化你的代码;你不需要adjA
或x
。你在哪里使用adjA
你应该只使用present_value
,你可以只迭代n
范围而不是x
:
def main():
investAmt = float(input("Enter investment amount:"))
i = float(input("Enter the expected investment growth rate:"))
d = float(input("Enter the discount rate:"))
print("Year \t Value at year($) \t Today's worth($)")
for n in range(1, 21):
expected_value = getExpInvestAmt(investAmt, i, n)
present_value = getPresentValue(expected_value, d, n)
print("{} \t {:,} \t {:,}".format(n, expected_value, present_value))
if present_value < 0.5 * investAmt:
break
要获得investAmt
10000 和i
5 的预期结果,您需要d
值 12.347528。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句