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

此代碼找到要發送包裹的價格。我不斷收到所有if語句的「IndexError:列表索引超出範圍」,我不知道爲什麼請幫助我。提前致謝 :)。IndexError:列表索引超出範圍(在一個數組上)

這是一個完整的代碼,但堆棧溢出說,這是太多的代碼,並沒有足夠的細節,所以我只是補充本款增加空間,讓喜歡你們問我可以添加完整的代碼。那麼你們怎麼樣?你們從哪裏來?你們多大了?爲什麼它仍然太少的文字和太多的代碼...什麼時候這會結束?

+0

通常這是(dums),因爲**索引超出範圍**。你確實知道'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(長度),這樣的:

>>> len(parcelWidth) 
4 

爲避免你的錯誤,不要訪問不存在的元素。可能你應該控制你的i有多高,並檢查所有數組是否有足夠的元素。

0

mr.Lewys在代碼中的問題爲u上述(I = I + 1)的for循環。 For循環會自動增加i直到它的最後一個索引號不需要增加(i)在for循環中,但是如果你想用while循環寫代碼,你必須增加i。代碼是

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的值。

希望這對你有用..

相關問題