2016-04-28 2 views
0

Ich schrieb einen Python-Code in Squish. Hier ist der Teil des Codes, wo es heißt, der Fehler ist: Was bedeutet das?Ich erhalte einen Fehler "Listenindex außerhalb des Bereichs" in Python

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

(columnEnd - columnStart) = 10

Start von for-Schleife

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") 
+1

Sie haben hier nur 5 Elemente '[" 1,6 "," 3,0 "," 7,0 "," null ", True,]' aber 'c' läuft von 0 bis 9, wenn es geht jenseits von 4 wirft er diesen Fehler auf 'array [r] [c]'. – AKS

+2

Guter Gott. Sobald dieses Programm behoben ist, würde ich dringend empfehlen, um Hilfe zu bitten, den Code auf [Code Review] (http://codereview.stackexchange.com/) zu organisieren. Du wirst sehr glücklich sein, dass du es getan hast. –

+0

Aber wenn ich laufe den Code unten seine adaequat: array = [[ "1,6", "3,0", "7,0", 'Null', True]] abc = 10 für c in Bereich (abc): ... wenn array [0] [1] = null: ... print 'Ture' ... else: ... print 'falsch' – Nitisha

Antwort

1

Ihr "Array" hat nur fünf Einträge in der ersten Position.

Aber weil Ihre (columnEnd - columnStart) ist 10, c ist im Bereich von 0 bis 9, also (0, 1, 2, 3, 4, 5, 6, 7, 8, 9). Wenn also c 5 erreicht, hat Ihr Array keinen Eintrag bei Index 5, und dann tritt der Fehler "Listenindex außerhalb des Bereichs" auf.

Wenn (columnEnd - columnStart) wäre 5 dann würde es funktionieren.

Wenn Sie in array[r] einer Schleife durch das gesamte Array wollen könnten Sie

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

sicher zu stellen, dass c in Bereich der Länge des Arrays und Array unterschiedliche Längen aufweisen können.

Verwandte Themen