2017-04-10 5 views
0
print("NOTE: Parcels can only be smaller than 100x100x100cm (WxLxH) and weight less than 20kg") 
parcelAmount = int(input("How many parcels are you sending?: ")) 
for i in range(parcelAmount): 
     parcelWidth.append(input("Please enter the width of the parcel " + str(i + 1) + ": ")) 
     parcelLength.append(input("Please enter the length of the parcel " + str(i + 1) + ": ")) 
     parcelHeight.append(input("Please enter the height of the parcel " + str(i + 1) + ": ")) 
     parcelWeight.append(input("please enter the weight of the parcel " + str(i + 1) + ": ")) 
     i = i + 1 
if float(parcelWidth[i]) or float(parcelLength[i]) or float(parcelHeight[i]) > int(100) or float(parcelWeight[i]) > int(20): 
    parcelRej = parcelRej + 1 
parcelAcc = parcelAmount - parcelRej 
if float(parcelWeight[i]) > 1 and float(parcelWeight[i]) < 5: 
    parcelPrice[i] = 10 
if float(parcelWeight[i]) > 5: 
    parcelPrice[i] = parcelWeight[i] - 5 + 10 
print("There are " + str(parcelRej) + " parcels rejected") 
print("There are " + str(parcelAcc) + " parcels accepted") 
print("It will cost $" + str(sum(parcelPrice)) + " To ship the parcel") 

Dieser Code gibt den Preis für das Senden von Paketen an. Ich bekomme "IndexError: Liste Index außerhalb des Bereichs" für alle if-Anweisungen und ich weiß nicht, warum bitte helfen Sie mir. Danke im Voraus :).IndexError: Listenindex außerhalb des gültigen Bereichs (auf einem Array)

Dies ist der vollständige Code, aber Stack-Überlauf sagt, es ist zu viel Code und nicht genug Details, so werde ich nur diesen Absatz hinzufügen, um Platz hinzuzufügen, so dass ich den vollständigen Code hinzufügen kann wie Sie gefragt. Also, wie geht es euch? Wo seid ihr her? Wie alt seid ihr? Warum ist es immer noch zu wenig Text und zu viel Code ... UGH wann wird das vorbei sein?

+0

Nun, normalerweise ist das (dums), weil ** der Index außerhalb des Bereichs liegt **. Wisst ihr sicher, dass der Wert von "i" zwischen "0" (inklusive) und "len (pracelLength)" liegt (exklusiv). –

+0

können Sie Ihren vollständigen Code zusammen mit Iterationen (for-Schleife, while-Schleife) anzeigen. – shiva

+0

atleast können Sie die Länge von Paketbreite, Paketlänge, Pakethöhe, Paketgewicht usw. angeben. – shiva

Antwort

0

Es sieht so aus, als ob Sie ein tieferes Verständnis dafür benötigen, was ein Array ist. Array hat seine Größe, Sie müssen also sicherstellen, dass Sie es nicht überschreiten. Nehmen Sie Ihre parcelWidth[i]. Ein Fehler außerhalb des Bereichs tritt auf, wenn i einen höheren Wert als die Größe parcelWidth -1 hat (weil das erste Element eines Arrays als 0 indiziert ist).

Beispiel:

>>> parcelWidth = [1,2,3,4] 
>>> i = 0 
>>> parcelWidth[i] 
1 
>>> i = 3 
>>> parcelWidth[i] 
4 
>>> i = 4 
>>> parcelWidth[i] 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
IndexError: list index out of range 
>>> 

Sie das Konzept jetzt bekommen Sie? Sie können nicht auf das Array-Element zugreifen, das nicht existiert.

schnell Um die Länge eines Arrays überprüfen Sie len können(), wie folgt aus:

>>> len(parcelWidth) 
4 

Ihre Fehler zu vermeiden Elemente greifen nicht, die nicht existieren. Wahrscheinlich solltest du kontrollieren wie hoch deine i geht und prüfen, ob alle Arrays genug Elemente haben.

0

mr.Lewys das Problem in Ihrem Code ist in For-Schleife erwähnt (i = i + 1). For-Schleife erhöht automatisch das i, bis es der letzte Index ist, keine Notwendigkeit, (i) in for-Schleife zu inkrementieren, aber wenn Sie Code schreiben möchten, während Sie while-Schleife verwenden, müssen Sie i erhöhen. Der Code ist

print("NOTE: Parcels can only be smaller than 100x100x100cm (WxLxH) 
and weight less than 20kg") 
parcelAmount = int(input("How many parcels are you sending?: ")) 
#parcelWidth=[] 
#parcelLength=[] 
#parcelHeight=[] 
#parcelWeight=[] 
#parcelRej=0 
#for i in range(parcelAmount): 
    parcelWidth.append(input("Please enter the width of the parcel " + 
    str(i + 1) + ": ")) 
    parcelLength.append(input("Please enter the length of the parcel " 
    + str(i + 1) + ": ")) 
    parcelHeight.append(input("Please enter the height of the parcel " 
    + str(i + 1) + ": ")) 
    parcelWeight.append(input("please enter the weight of the parcel " 
    + str(i + 1) + ": ")) 
    # i = i + 1 
if float(parcelWidth[i]) or float(parcelLength[i]) or float(parcelHeight[i]) > 100 or float(parcelWeight[i]) > 20: 
    parcelRej = parcelRej + 1 
parcelAcc = parcelAmount - parcelRej 
if float(parcelWeight[i]) > 1 and float(parcelWeight[i]) < 5: 
    parcelPrice[i] = 10 
if float(parcelWeight[i]) > 5: 
    parcelPrice[i] = parcelWeight[i] - 5 + 10 
print("There are " + str(parcelRej) + " parcels rejected") 
print("There are " + str(parcelAcc) + " parcels accepted") 
print("It will cost $" + str(sum(parcelPrice)) + " To ship the parcel") 

und ich bekomme Fehler als PaketPreis ist nicht definiert, da IDK die Werte davon.

Hoffe, das ist nützlich für Sie ..

Verwandte Themen