0

这里有很多代码

...

if c==excelfiles[1]:
    b ==excelfiles[1]
    wb = xlrd.open_workbook(a)
    wb.sheet_names()
    sh = wb.sheet_by_index(0)
    for rownum in range(sh.nrows):
        print sh.row_values(rownum)

for i in range(sheet.nrows):
    cashflow = [sheet.cell_value(i,0)],[sheet.cell_value(i,1)],[sheet.cell_value(i,2)]
    print cashflow

def npv(rate, cashflow):
    value = cashflow[1] * ((1 + rate/100) ** (12 - cashflow[0]))
    FV = 0
    FV += value
    return FV

def irr(cashflow, iterations = 100):
    rate = 1.0

    i = 0
    while (cashflow[0][1] == 0):
        i += 1

    investment = cashflow[i][1]

    for i in range(1, iterations+1):
        rate *= (1 - (npv(rate, cashflow) / investment))
        return rate

r = irr(cashflow)

print r

错误/输出:

  File "<pyshell#90>", line 1, in <module>
    import quarterZ
  File "quarterZ.py", line 65, in <module>
    r = irr(cashflow) # why is list index out of range?
  File "quarterZ.py", line 56, in irr
    while (cashflow[0][1] == 0):
IndexError: list index out of range

有人可以解释为什么我的列表索引超出范围吗?你能告诉我如何解决这个问题吗?我对python比较陌生,所以我确定这是一个愚蠢的错误。

非常感谢!

我还在这里附上了代码:http: //ideone.com/G5hGuK

4

1 回答 1

0

cashflow[0]在第 10 行的 for 循环中设置为[sheet.cell_value(i,0)],这是一个长度为 1 的列表。表达式cashflow[0][1]正在尝试读取该列表中的第二个值。

于 2013-10-06T22:36:11.647 回答