2017-03-04 61 views
-1

我目前正在測試我的程序,它基本上是用python中的CSV模塊讀取和寫入「stock」文件。但是我得到這個錯誤:
IndexError: list index out of rangeIndexError:列表索引超出範圍 - Python 3.5.2

錯誤來自於這部分程序:

def GTIN8Test(): # function validates GTIN 
digit8 = input("Please enter the GTIN-8 of the product you want to modify in the stock file: ") # input for GTIN 
while digit8.isdigit is False or len(digit8) > 8 or len(digit8) < 8: # checks length of GTIN and characters 
    digit8 = input("Invalid GTIN-8! Please enter a valid GTIN ") 
V1 = int(digit8[0]) 
V2 = int(digit8[1]) 
V3 = int(digit8[2]) 
V4 = int(digit8[3]) # validates GTIN 
V5 = int(digit8[4]) 
V6 = int(digit8[5]) 
V7 = int(digit8[6]) 
V8 = int(digit8[7]) 
V1a = V1 * 3 
V3a = V3 * 3 
V5a = V5 * 3 
V7a = V7 * 3 
T1 = V1a + V2 + V3a + V4 + V5a + V6 + V7a + V8 
T2 = T1 % 10 
if T2 != 0: 
    print("Your GTIN-8 code",digit8,"is not valid!") 
    GTIN8Test() # calls function so user can enter another GTIN if theirs is invalid 
else: 
    import csv # imports CSV for reading files 
    s = open("stock.txt") # opens stock file 
    counter = 0 # sets counter to 0 
    while counter != 3: # while counter is not = to 3 this loop keeps running 
     for stock in csv.reader(s): # checks each line in the stock file6 
      if digit8 == stock[0]: 
       print("GTIN-8 is valid and is found in the stock file") 
       StockMod(digit8) # StockMod is called, carring the GTIN as a paramter 
      else: 
       counter = counter + 1 # if no matches on a line are found 1 is added to the counter. 
    print("GTIN valid but not found in stock file!") # message displays when counter = 3. 
    s.close() # closes stock file  

和錯誤似乎在這個函數結束時被觸發:

def StockMod(digit8): # function which modifies the stock file, digit8 taken as a paramter 
import csv # imports CSV to read and write 
StockFile = [] # creates stock file list 
s = open("stock.txt") # opens stock file 
for stock in csv.reader(s): # checks each line in the stock file 
    if digit8 == stock[0]: # if the GTIN matches another GTIN in the stock file 
     print(digit8, "is assigned to", stock[1], "and has", stock[2], "in stock with a target stock level of", stock[4]) 
     quantity = input("Please enter quantity") # asks user for quantity 
     newq = int(quantity) + int(stock[2]) # calculates the new quantity of the product chosen by the user 
     stock[2] = newq # changes the quantity in the file to the new one 
    StockFile.append(stock) # appends the list stockfile with the information gathered by the program   
s.close() # closes the file 
print(StockFile) # prints stockfile list 
s = open("stock.txt", "wt", newline = "") # opens the stock file again for writing 
writer = csv.writer(s) # sets up csv to write 
for product in StockFile: # goes through each line in the stockfile list 
    writer.writerow(product) # writes everything back into the stock file 
s.close() # closes the stock file 
print("File write success") 

對不起,如果有任何格式錯誤!

在此先感謝。

編輯:

以下是完整的錯誤:

Traceback (most recent call last): File "C:\Users\Mavro\Documents\School Work\GCSE COURSEWORK\Computer Science\Task 3\Task3MOD (1).py", line 84, in <module> GTIN8Test() # calls GTIN8Test for StockMod File "C:\Users\Mavro\Documents\School Work\GCSE COURSEWORK\Computer Science\Task 3\Task3MOD (1).py", line 29, in GTIN8Test if digit8 == stock[0]: IndexError: list index out of range

+0

請給出錯誤的完整追蹤。 – cdarke

+0

嗨,我剛添加它。 – Mavro

+0

您應該檢查csv.reader(s)中每一行庫存的輸出:'。 '股票[0]'似乎會拋出你的錯誤。也許你的輸入文檔中有空行? – rinderwahn

回答

0

怎樣的 「stock.txt中」 是什麼樣子?也許你在文件的開頭有空行?在這種情況下,csv.reader將返回空列表。在這種情況下,你可以在此行之前添加s.readline():

for stock in csv.reader(s): 

,你也可以檢查每一行,如果它不是空:

if stock != []: 
相關問題