【问题标题】:How to loop with two conditions, in python?如何在python中循环两个条件?
【发布时间】: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


【解决方案1】:

我认为你可以简化你的代码;你不需要adjAx。在您使用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 

要获得 10000 的 investAmt 和 5 的 i 的预期结果,您需要 d 值为 12.347528。

【讨论】:

  • 感谢您的成功!但我希望你能解释实际发生的事情或背后的逻辑,当你分配 present_value =investAmt.事实上,我是 python 和编程新手,非常感谢您的帮助。
  • @ArifRashed 我只是将investAmt 的值分配给present_value 以确保循环中的if 测试具有有效数据以在第一次迭代时进行比较。您实际上可以删除该分配并将if 语句放在print 之后。
  • 打印后放置也可以!我的理解是 python 会逐行运行代码。我以为它会迭代 20 次然后意识到有一个 If 语句。
  • @ArifRashed 只要if 在循环中(即与其他语句相同的缩进),它将在每次迭代中执行。因为那是把if 放在那里的更好的代码,所以我会编辑答案。
猜你喜欢
  • 2014-02-13
  • 2020-04-29
  • 1970-01-01
  • 2020-05-27
  • 1970-01-01
  • 2017-03-04
  • 2011-05-18
  • 2021-05-29
  • 2021-11-29
相关资源
最近更新 更多