Python 中的附息债券计算器
Coupon Bond Calculator in Python
我正在尝试进行息票债券计算,并在下面的代码中将 运行 保留为 "Type Error must be str, not int"。我想不出在哪里使它成为一个字符串。
"""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)))
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 = int(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: "))
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 一个字符串——这是在文档和教程中定义的。您尝试对字符串进行计算;我遇到了几个不同的错误——包括你的第一行 input
中缺少的 rparen——但不是你引用的错误。在任何情况下,您都需要根据需要将输入值从 str
转换为 int
和 float
Egbert 几乎是正确的;美元金额应该是浮点数:
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: "))
在之后,你需要修复你构建的奇怪的无限循环:
while j <= n:
由于j
和n
在这个循环中永远不会改变,一旦你进入它就是无限的。最重要的是,紧随其后的 for
循环似乎旨在执行相同的迭代。
完全删除 while
;我认为更改后我看到的结果是正确的。
我正在尝试进行息票债券计算,并在下面的代码中将 运行 保留为 "Type Error must be str, not int"。我想不出在哪里使它成为一个字符串。
"""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)))
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 = int(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: "))
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 一个字符串——这是在文档和教程中定义的。您尝试对字符串进行计算;我遇到了几个不同的错误——包括你的第一行 input
中缺少的 rparen——但不是你引用的错误。在任何情况下,您都需要根据需要将输入值从 str
转换为 int
和 float
Egbert 几乎是正确的;美元金额应该是浮点数:
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: "))
在之后,你需要修复你构建的奇怪的无限循环:
while j <= n:
由于j
和n
在这个循环中永远不会改变,一旦你进入它就是无限的。最重要的是,紧随其后的 for
循环似乎旨在执行相同的迭代。
完全删除 while
;我认为更改后我看到的结果是正确的。