현재 파이썬에서 CSV 모듈을 사용하여 'stock'파일을 읽고 쓰는 것을 의미하는 프로그램을 테스트하고 있습니다. 그러나이 오류가 무엇입니까 :
IndexError: list index out of range
IndexError :리스트 인덱스가 범위를 벗어남 - 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
오류의 전체 역 추적을주십시오. – cdarke
안녕하세요, 방금 추가했습니다. – Mavro
csv.reader (s)에서'for stock의 각 행에 대한 출력을 확인해야합니다 :'. 'stock ['[0]'는 당신의 실수를 던질 것 같습니다. 아마도 입력 문서에 빈 줄이 있습니까?! – rinderwahn