現在、私は基本的には、読み込みおよび書き出しを意味するプログラムをPythonのCSVモジュールを使って 'ストック'ファイルとしてテストしています。しかし、私はこのエラーを取得しています:
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