简体   繁体   English

有索引错误,我不知道如何纠正

[英]Have an index error I don't know how to correct

I found this code elsewhere on the site, but for some reason I keep getting the same error message: 我在网站的其他位置找到了此代码,但是由于某种原因,我不断收到相同的错误消息:

products[row[0]] = [row[1], row[2], row[3]]
IndexError: list index out of range.

I am unsure how to correct this, any help is appreciated, thanks. 我不确定如何解决此问题,感谢您的帮助。

This is the code: 这是代码:

MAX_FIELD_LEN = 8

def main():
    products = {}
    product_location  = {}
    location = 0
    # This is the file directory being made.
    with open('stockfile.txt', 'r+') as f:
        # This is my file being opened.

        for line in f:
            # keep track of each products location in file  to overwrite with New_Stock
            product_location[line.split(',')[0]] = location
            location += len(line)
            # Need to strip to eliminate end of line character
            line = line[:-1]
            # This gets rid of the character which shows and end of line '\n'
            row = line.split(',')
            # The row is split by the comma
            products[row[0]] = [row[1], row[2], row[3]]
            # The products are equal to row 1 and row 2 and row 3. The GTIN is going to take the values of the product and price so GTIN 12345678 is going to correspond to Fridge and 1.

        print(products)
        total = 0

        while True:
            GTIN = input('Please input GTIN: ')
            # To terminate user input, they just need to press ENTER
            if GTIN == "":
                break
            if (GTIN not in products):
                print('Sorry your code was invalid, try again:')
                break

            row = products[GTIN]
            description, value, stock = row
            print('Stock data: ')
            print('GTIN \t\tDesc. \t\tStock \t\tValue')
            print(GTIN,'\t',description,'\t', stock, '\t', value)

            quantity = input('Please also input your quantity required: ')
            row[2] = str(int(stock) - int(quantity))
            product_total = int(quantity) * int(value)
            for i in range(len(row)):  
                row[i]  = row[i].rjust(MAX_FIELD_LEN)
            New_Stock = GTIN.rjust(MAX_FIELD_LEN) + ',' + ','.join(row) + '\n'
            #print(New_Stock, len(New_Stock))
            f.seek(product_location[GTIN])
            f.write(New_Stock)
            print('You bought: {0} {1} \nCost: {2}'.format(GTIN, description, product_total))

            total = total + product_total
        f.close()
        print('Total of the order is £%s' % total)

main()

Please check your input file 'stockfile.txt', at least one line of your file don't have 3 or more ",". 请检查您的输入文件'stockfile.txt',文件的至少一行没有3个或更多的“,”。 Or you have some blank lines between your data. 或者您的数据之间有一些空白行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何摆脱这个索引超出范围错误? 知道原因但不知道解决办法 - How to get rid of this index out of range error? I know the reason but don't know the solution 我有这个错误,我不知道该怎么办 - I have this error and I don't know what to do 我有错误 TypeError: unsupported operand type(s) for /: 'function' and 'int' 我不知道如何修复它 - I have the error, TypeError: unsupported operand type(s) for /: 'function' and 'int' and I don't know how to fix it 我有一个 python TypeError,我不知道如何修复它 - I have a python TypeError and I don't know how to fix it 列表索引超出范围:我不知道如何解决 - List index out of range: I don't know how to solve it 由于某种原因,在我的代码中出现“索引超出范围”的错误,我实际上刚刚开始使用 Python,因此不知道如何解决这个问题 - In My Code There Is A Error Saying "Index Out Of Range" For Some Reason, I Actually Just Started Python And Hence Don't Know How To Solve This 我收到EOF错误,不知道如何解决 - I am getting an EOF error and don't know how to fix it libtorrent API给出错误,我不知道如何解释 - libtorrent API gives error I don't know how to interpret 我不知道如何修复一元 +: 'str' 错误 - I don't know how to fix an unary +: 'str' error Python:我的代码出错,我不知道如何修复它 - Python: Error in my code and i don't know how to fix it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM