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语句,我不知道为什么请帮助我。提前感谢:)。在
这是完整的代码,但堆栈溢出说这是太多的代码和不够的细节,所以我将只添加这一段,以增加空间,以便我可以添加完整的代码,如你们所要求的。你们怎么样?你们从哪来的?你们几岁了?为什么它仍然太少的文本和太多的代码。。。呃什么时候结束?在
看起来你需要更深入地了解数组是什么。数组有它的大小,所以你需要确保你没有超过它。带上你的
parcelWidth[i]
。当i
的值大于parcelWidth
-1的大小时,会发生索引超出范围错误(这是因为数组的第一个元素被索引为0)。在示例:
你现在明白了吗?不能访问不存在的数组元素。在
要快速检查数组的长度,可以使用len(),如下所示:
^{pr2}$为了避免错误,不要访问不存在的元素。也许您应该控制
i
的高度,并检查所有数组是否都有足够的元素。在在刘易斯先生代码中的问题是在for循环中提到的(i=i+1)。For循环将自动递增i直到它的最后一个索引不需要在For循环中增加(i),但是如果你想用while循环编写代码,你必须增加i
我得到了错误,因为parcelPrice没有被定义,因为它的值。在
希望这对你有用。。在
相关问题 更多 >
编程相关推荐