绑定CF进行循环,如果不是则循环 [英] Bond CF for loop and if else loop

查看:44
本文介绍了绑定CF进行循环,如果不是则循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 if/else 循环将最后一笔现金流加回到 par 值中,但是我似乎做不到.如何为范围内的特定项目分配 int ?我正在尝试这样做,如果索引> 10,它将重新添加par值.

I am trying to add the last cash flow back into the par value using a if/else loop, but I can't seem to do it. How do I assign an int to a specific item in the range? I am trying to make it so if the index > 10, it will add the par value back in.

par = 1000
coupon_rate = 3
T = 5
freq = 2


def cf_calculator(par, r, T, freq):
    for i in range(T * freq):
        if (T) < (T * freq):
            coupon = (r/100) * par/freq
            print(coupon)
        else: 
            coupon = (r/100) * par/freq + par
            print(coupon)



print(cf_calculator(1000,3,5,2))

我知道我的if循环是错误的.有更好的方法吗?

I know my if loop is wrong. Is there a better way?

推荐答案

我认为这就是您的意思:

I assume this is what you are meaning to do:

par = 1000
coupon_rate = 3
T = 5
freq = 2


def cf_calculator(par, r, T, freq):
    for i in range(0,(T * freq)+1):
        if (i) < (T * freq):
            coupon = (r/100) * par/freq
            print(coupon)
        else: 
            coupon = (r/100) * par/freq + par
            print(coupon)



print(cf_calculator(1000,3,5,2))

输出:

15.0
15.0
15.0
15.0
15.0
15.0
15.0
15.0
15.0
15.0
1015.0
None

现在,根据函数的名称,您只需要使用相应的折现率对每个现金流进行折现.然后,您可以将所有折现现金流量加在一起以获得债券的现值(您可能希望函数返回).

Now, according to the name of the function, you just need to discount each cash flow with its corresponding discount rate. Afterwards, you can add all of your discounted cash flows together in order to obtain the present value of the bond (which you probably want the function to return).

此外,我将重写代码以使其更具可读性:

Additionally, I would rewrite the code a bit to make it more readable:

par = 1000
coupon_rate = 3
T = 5
freq = 2


def cf_calculator(par, r, T, freq):
    for i in range(0,(T * freq)+1):
        if i < (T * freq):
            coupon = ((r/100) * par) / freq
            print(coupon)
        else: 
            coupon = (((r/100) * par) / freq) + par
            print(coupon)

print(cf_calculator(par,coupon_rate,T,freq))

这篇关于绑定CF进行循环,如果不是则循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆