简体   繁体   English

IndexError:python 上的列表索引超出范围错误

[英]IndexError: list index out of range error on python

This is my code:这是我的代码:

while True:
print("Type 'done' to finish receipts")
code=input("Enter item code to add:")
items =open("items.txt")
quant=input("Quantity:")
details = items.read()
detailslist = details.split("\n")
for a in detailslist:
    fire = a.split("#")
    print (fire)
    b = fire[0]
    c = fire[1]
    d = fire[2]
    dictd = {}
    dictd[b] = c + ' ' +' Quantity: '+ ' ' + quant +' '+ 'Price:' + d
    print (dictd)       

This is in items.txt:这是在 items.txt 中:

A# Almanac2018# 18.30
B# Almanac2020# 23.80
C# Almanac2021# 16.54
D# Almanac2022# 22.25

I am getting this error:我收到此错误:

Type 'done' to finish receipts
Enter item code to add:A
Quantity:45
['A', ' Almanac2018', ' 18.30']
{'A': ' Almanac2018  Quantity:  45 Price: 18.30'}
['B', ' Almanac2020', ' 23.80']
{'B': ' Almanac2020  Quantity:  45 Price: 23.80'}
['C', ' Almanac2021', ' 16.54']
{'C': ' Almanac2021  Quantity:  45 Price: 16.54'}
['D', ' Almanac2022', ' 22.25']
{'D': ' Almanac2022  Quantity:  45 Price: 22.25'}
['']
Traceback (most recent call last):
  File "receipt.py", line 12, in <module>
    c = fire[1]
IndexError: list index out of range

I am trying to make a program that makes a receipt of items so if you could provide any code that would be helpful.我正在尝试制作一个接收物品的程序,所以如果你能提供任何有用的代码。 Any help would be appreciated.任何帮助,将不胜感激。

The problem you have in your code is with the empty strings in your items.txt file.您在代码中遇到的问题是items.txt文件中的空字符串。 When there's an empty string, fire would resolve to [''] , which is a list of only 1 item, so you get an error when the code tries to run c = fire[1] .当有一个空字符串时, fire将解析为[''] ,这是一个仅包含 1 个项目的列表,因此当代码尝试运行c = fire[1]时会出现错误。 You can add a check to see if it's an empty line or not:您可以添加检查以查看它是否为空行:

for a in detailslist:
    fire = a.split("#")
    if len(fire) > 1:
        print (fire)
        b = fire[0]
        c = fire[1]
        d = fire[2]
        dictd = {}
        dictd[b] = c + ' ' +' Quantity: '+ ' ' + quant +' '+ 'Price:' + d
        print (dictd)

Give a man a fish, feed him for a day... teach a man to fish.给一个人一条鱼,喂他一天……教一个人钓鱼。

So this isn't a fish, but it is a fishing rod (as long as your using Python v3.7 or greater):所以这不是鱼,而是钓鱼竿(只要您使用 Python v3.7 或更高版本):

import pdb #Only need this if python < v3.7

while True:
print("Type 'done' to finish receipts")
code=input("Enter item code to add:")
items =open("items.txt")
quant=input("Quantity:")
details = items.read()
detailslist = details.split("\n")
for a in detailslist:
    fire = a.split("#")
    print (fire)
    try:
        b = fire[0]
        c = fire[1]
        d = fire[2]
        dictd = {}
        dictd[b] = c + ' ' +' Quantity: '+ ' ' + quant +' '+ 'Price:' + d
        print (dictd)
    except:
        breakpoint()  # if Python >= v3.7
        pdb.set_trace()  # if Python < v3.7

Which means you can see the exact shape and size of "fire" when the error is being caused.这意味着您可以在导致错误时看到“火”的确切形状和大小。 Should help your debugging efforts here - and in future when other problems inevitably crop up.应该有助于您在这里的调试工作 - 以及将来不可避免地出现其他问题时。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM