2017-04-10 10 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") 

このコードは、パーセルを送信する価格を検出します。私は "IndexError:リストのインデックスを範囲外に"取得し続けます。なぜなら、私はなぜ私を助けてくれるのかわかりません。前もって感謝します :)。IndexError:リストのインデックスが範囲外(リスト上)

これは完全なコードですが、スタックオーバーフローはあまりにも多くのコードであり、詳細が不十分だと言っていますので、スペースを追加するためにこの段落を追加します。あなたはどうやってやっていますか?あなたはどこから来たの?君たち歳いくつ?なぜそれはまだ小さすぎるテキストとあまりにも多くのコードです... UGHこれはいつ終わるでしょうか?

+0

インデックスは範囲外です**ので、通常(ダム)です。 'i'の値が' 0'(両端を含む)と 'len(pracelLength)'(排他的)の間にあることをあなたは知っていますか? –

+0

あなたは完全なコードを反復とともに表示できます(for-loop、whileループ)。 – shiva

+0

少なくともparcelWidth、parcelLength、parcelHeight、parcelWeight ..の長さを指定できます。 – shiva

答えて

0

配列が何であるかをより深く理解する必要があるようです。配列にはサイズがあるので、それを超えていないことを確認する必要があります。 parcelWidth[i]に行ってください。 iのサイズがparcelWidth -1より大きい場合(つまり、配列の最初の要素が0としてインデックス付けされているため)、範囲外のインデックスのエラーが発生します。

例:

>>> 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 
>>> 

あなたは今概念を得るのですか?存在しない配列要素にアクセスすることはできません。存在しない要素にアクセスしていない、あなたのエラーを回避するには

>>> len(parcelWidth) 
4 

:すぐにあなたがそのように、LEN()を使用することができ、配列の長さをチェックするために

。おそらく、iの高さを制御し、すべての配列に十分な要素があるかどうかを確認する必要があります。

0

mr.Lewysあなたのコードの問題は、forループで(i = i + 1)言及されています。 Forループは自動的にインクリメントしますが、最後のインデックスまで増分する必要はありません。forループではなく、whileループを使用してコードを書きたい場合は、インクリメントする必要があります。コードは

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

であり、parcelPriceが定義されていないため、idkの値に誤りがあります。

希望すると便利です。

関連する問題