I'm trying to do coupon bond calculations and keep running into "Type Error must be str, not int" in the below code. I cannot figure out where to make it a string.
"""The equation is the (sum from j to n of (c/(1+i)^j))+ (f/(1+i)^n)"""
print('What Are You Trying To Solve For?')
startvariable = str(input(' Price (p), Face (f), Years to Maturity (n), Interest Rate (i), Current Yield (cy), YTM (Y)' ).lower() )
while startvariable == 'p':
f = (input("Face or Par Value (number only): "))
i = (input("Interest Rate (as decimal): "))
c = (input("Coupon (number only): "))
n = (input("n value: "))
j = (input("j (starting) value: "))
summation_value = 0
while j <= n:
for k in range (j, (n + 1)):
add_me = (c/(1+i)** j)
summation_value += add_me
k += 1
print('Bond Price: ', (summation_value + ((f) / (1 + i) ** n)))
input
returns a string -- this is defined in the documentation and tutorials. You try to do computations on a string; I get a couple of different errors -- including the missing rparen on your first input
line -- but not the one you cite. In any case, you need to cast your input values from str
to int
and float
, as needed
Egbert has them almost correct; the dollar amounts should be floats:
f = float(input("Face or Par Value (number only): "))
i = float(input("Interest Rate (as decimal): "))
c = int(input("Coupon (number only): "))
n = int(input("n value: "))
j = int(input("j (starting) value: "))
After this, you need to fix the curious infinite loop you built:
while j <= n:
Since j
and n
never change within this loop, it's infinite once you enter. Most of all, it appears that the for
loop immediately after is intended to perform the same iteration.
Remove the while
altogether; I think what I'm seeing after that change is the correct result.