【发布时间】:2020-08-16 23:17:46
【问题描述】:
我需要编写一个代码来显示 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?它的值在开始时为 0,因此您的“if”子句为真,循环中断。
-
我认为您的预期结果不正确; 1年后投资价值10500,按6.25%折现应该是9882,而不是9346
-
adjA 为调整量,即现值。我的想法是,在计算出现值之后,adjA += 1 会将现值的值赋予 adjA = 0。因此,当 adjA = 0 = 到 .5 * 初始值时,我的 IF 语句会中断。
-
我也知道预期的结果可能是错误的,但这只是为了澄清我的意思是什么时候程序直到第 20 年价值达到初始投资的 50% 时才会循环.
标签: python loops for-loop if-statement python-requests