我看了這個問題上的其他堆棧溢出帖子,但我仍然不明白這個程序我試圖做的錯誤。我不明白爲什麼List索引超出for循環中的if語句的範圍。請有人解釋給我,以及要改變什麼來解決它。列表索引超出範圍if語句
order = ["12345678", "2", "12345670", "2", "11111111", "3", "87654321", "8"]
orderCount = 0
productCount = 0
file = open("file.txt", "r")
print(len(order))
while orderCount < len(order):
for line in file:
product = line.split(",")
print(orderCount)
if order[orderCount] == product[0]:
totalCost = float(order[1]) * float(product[2].strip('\n'))
receipt = product[productCount], product[1], order[1], product[2].strip('\n'), str(totalCost)
receipt = " ".join(receipt)
print(receipt)
else:
print("Product not found.")
orderCount += 2
'orderCount'大於'order'的最大索引。你試圖用while循環來阻止它,但問自己:循環檢查'orderCount'的大小是多少,'orderCount'的大小是多少 –