1

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))

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

aschultz
  • 1,658
  • 3
  • 20
  • 30
PythonCub
  • 19
  • 3

1 Answers1

1

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))

Output:

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))
Menno Van Dijk
  • 863
  • 6
  • 24