简体   繁体   中英

I am getting an error "list index out of range" in Python

I wrote a Python code in Squish. Here is the piece of code where it says the error is: What does this mean?

array = [["1,6", "3,0", "7,0", 'null', True,]]

(columnEnd - columnStart) = 10

start of for loop

for r in range(len(array)):
        waitForObjectItem(object_id, str(r + rowStart) + "/" + str(columnStart))
        clickItem(waitForObject(object_id), str(r + rowStart) + "/" + str(columnStart), 0, 0, 0, Qt.LeftButton);

        for c in range(columnEnd - columnStart)
            # Getting an error at this point , if loop
            if array[r][c] != 'null':
                print "array index is : {}".format(array[r][c])
                print "row is {}".format(r)
                print "column is {}".format(c)
                dataType = array[r][c].__class__
                print "dataType is {}".format(dataType)
                checkState = item_checks(object_id, r + rowStart, c + columnStart).checkState;               
                print "checkstate is {}".format(checkState)
                if (dataType == str and (checkState == "uncheckable" or checkState == "unknown")):
                    waitForObjectItem(object_id, str(r + rowStart) + "/" + str(c + columnStart))
                    doubleClickItem(waitForObject(object_id), str(r + rowStart) + "/" + str(c + columnStart), 1, 1, 0, Qt.LeftButton)
                    widget = "{type='QWidget' unnamed='1' container='" + object_id + "'}";
                    txt = array[r][c]
                    txtString = str(txt)
                    type(waitForObject(widget), "<DEL>")
                    type(waitForObject(widget), str(array[r][c]))
                    try:
                          type(waitForObject(widget), str("<TAB>"))
                    except Exception(err):
                        raise Exception ("Error is found :- {}".format(err))
                elif ((array[r][c] == True or array[r][c] == False) and (checkState != "uncheckable" and checkState != "unknown")):
                    print "data type boolean loop"
                    waitForObjectItem(object_id, str(r + rowStart) + "/" + str(c + columnStart))
                    if (array[r][c] != (item_checks(object_id, r + rowStart, c + columnStart).checkState == "checked")):
                        rowHeight = waitForObject(object_id).rowHeight(r + rowStart)
                        clickItem(waitForObject(object_id), str(r + rowStart) + "/" + str(c + columnStart), 10, rowHeight / 2, 0, Qt.LeftButton)
                        
                else:
                    raise Exception(object_id + ": dataType '" + str(dataType) + "' doesen't match to expected one of cell or is unknown or unhandled")

Your "array" has just five entries in the first position.

But because your (columnEnd - columnStart) is 10, c is in range from 0 to 9, so (0, 1, 2, 3, 4, 5, 6, 7, 8, 9). So when c reaches 5, your array has no entry at index 5 and then the "list index out of range" error occurs.

If (columnEnd - columnStart) would be 5 then it would work.

If you want to loop through the whole array in array[r] you could use

for c in range(len(array[r])):
    if(array[r][c] ...):

to make sure that c is in range of the length of your array and your array can have different lengths.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM