IRR calcluation : ValueError: invalid literal for float():

IRR calcluation : ValueError: invalid literal for float():

我正在尝试计算 IRR 并在命令行中给出结果,但每当我尝试将这些值作为参数传递时,它都会因以下错误而失败

代码

import numpy
import sys   

def irrYearly(array):
    array = [array]
    irr = round(numpy.irr(array), 2)*100
    return irr
def irrCal(tenure, array):
    if tenure == "Yearly":
        # return irrYearly(array)
        # array = [-30000, -30000, -30000, -30000, -30000, 107180]  -- if I uncomment this line its works fine
        print(irrYearly(array))

    elif tenure == "Monthly":
        # return irrMonthly(array)
        print(irrMonthly(array))

def main():
    if len(sys.argv) == 3:
        tenure = sys.argv[1]
        array = sys.argv[2]
        irrCal(tenure, array)  

main()

错误

请找出错误

C:\Python27\python.exe C:/Users/abhis/PycharmProjects/IRR/irr.py Yearly -30000.00,-30000.00,-30000.00,-30000.00,-30000.00,107180.00
Traceback (most recent call last):
  File "C:/Users/abhis/PycharmProjects/IRR/irr.py", line 74, in <module>
['-30000.00,-30000.00,-30000.00,-30000.00,-30000.00,107180.00']
    main()
  File "C:/Users/abhis/PycharmProjects/IRR/irr.py", line 64, in main
    irrCal(tenure, array)
  File "C:/Users/abhis/PycharmProjects/IRR/irr.py", line 49, in irrCal
    print(irrYearly(array))
  File "C:/Users/abhis/PycharmProjects/IRR/irr.py"", line 19, in irrYearly
    irr = round(numpy.irr(array), 2)*100
  File "C:\Python27\lib\site-packages\numpy\lib\financial.py", line 669, in irr
    res = np.roots(values[::-1])
  File "C:\Python27\lib\site-packages\numpy\lib\polynomial.py", line 222, in roots
    p = p.astype(float)
ValueError: invalid literal for float(): -30000.00,-30000.00,-30000.00,-30000.00,-30000.00,107180.00

argv[2] 是一个用逗号分隔值的字符串,但对于 numpy 它是一个字符串而不是浮点数列表。您首先需要像这样转换它: float_vals = [float(x) for x in argv[2].split(",")].