如何在python中循环使用两个条件?

阿里夫·拉希德

我需要编写一个代码来显示 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.
缺口

我认为你可以简化你的代码;你不需要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 

要获得investAmt10000 和i5 的预期结果,您需要d值 12.347528。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在python中的for循环中同时检查两个条件?

如何在python中的for循环下使用两个不同的循环

如何使用python中的列表在两个条件下进行循环

如何在Python中实现这两个循环?

Python:如何在while循环中满足两个完全不同的条件中的一个或两个

如何在while循环的一个结果集中使用两个条件?

如何使用两个条件(大于和小于)来终止 Java 中的 for 循环?

如何在 JavaScript 中合并两个 for..of 循环?

如何在bash中同时重复两个for循环?

如何在函数中的两个列表之间循环?

如何在javascript中循环两个对象?

如何在两个for循环中使用break?

如何在for循环中使用两个变量?

如何在ps中逻辑与两个选择条件?

如何在JAVA中按两个条件排序?

如何在列表理解Python中构建两个for循环

如何在列表理解Python中构建两个for循环

如何在python中的单个for循环中执行两个功能

如何在SQL中使用两个where条件?

如何在两个条件下使用

如何在python中使用for循环求和两个列表项的乘积?

如何在导轨中仅使用一个for循环同时遍历两个数组

如何在JavaScript中为两个数组的每个循环使用一个?

如何在Python中编写以两个变量(列)为条件的lambda函数

如何在python中根据条件添加两个后续列表项

如何在 Laravel 中的单列上使用具有两个条件的 orWhere

如何在 UNION 查询中的两个表中使用 where 条件?

如何在SQL中的某些条件下使用外键链接两个表?

SQL如何在两个不同的列上使用两个不同的条件带来两个结果