Python coding - list index out of range
Question: lots of code up here

...

    
    
    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
    

Error/Output:

    
    
      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
    

Can someone please explain why my list index is out of range? And can you show
me how to fix this? I am relatively new to python so I'm sure it's a stupid
mistake.

Thanks so much!

I've also attached the code here: <http://ideone.com/G5hGuK>


Answer: `cashflow[0]` is set to `[sheet.cell_value(i,0)]` in your for loop on line 10,
which is a list with length 1. The expression `cashflow[0][1]` is trying to
read the second value in that list.

