python中的几何序列

时间:2017-11-03 11:41:29

标签: python python-3.x math

如何使用投资5000的几何序列,每年增加5%的新值?我一直在尝试一切,但我无法得到正确答案。请帮忙

所以今年: 0 = 5000

  1. 5250
  2. 5512.5
  3. 5788.13
  4. 到目前为止

    代码:

     public IActionResult Show(string generic_se_name)
        {
            var type = db.UrlRecords.FirstOrDefault(x => x.Slug == generic_se_name);
            if (type != null)
            {
                switch (type.EntityType)
                {
                    case "Product":
                        return RedirectToRoute("Product", new {productName = generic_se_name });
                    case "Category":
                        return RedirectToRoute("Category", new { categoryName = generic_se_name });
                }
            }
            return BadRequest();
        }
    

2 个答案:

答案 0 :(得分:0)

问题在于您的打印声明。

你有两个选择让它运作:

  1. 列表理解,创建完整的利润列表,然后用年数拉链

    def calc_profits(principal, year): INTREST_GROWTH = 0.05 profits = [principal * ((1+INTREST_GROWTH) ** (y - 1)) for y in range(1, year + 1)] for i,p in zip(range(year),profits): print("{0}:{1:.2f}".format(i, p))

  2. 迭代给出一个新值并为每次循环迭代打印

    def calc_profits(principal, year): INTREST_GROWTH = 0.05 profits = principal for y in range(1, year): profits = profits*(INTREST_GROWTH+1) print("{0}:{1:.2f}".format(y, profits))

答案 1 :(得分:-1)

您可以计算投资的最终价值,并拿走本金以了解您获得了多少利润。

def calc_profits(principal, year):
    INTEREST_GROWTH = 0.05
    end_capital = principal * (1 + INTEREST_GROWTH) ** year
    profit = end_capital - principal
    return profit

编辑:如果你想知道每年的利润,你可以用列表推导来做。

def calc_profits(principal, year):
    INTEREST_GROWTH = 0.05
    profit = [principal * (1 + INTEREST_GROWTH) ** y - principal
               for y in range(1, year + 1)]
    return profit