Python3.4-运行脚本时发生列表索引超出范围错误,但在解释器中不发生

Python3.4-运行脚本时发生列表索引超出范围错误,但在解释器中不发生,python,list,pycharm,Python,List,Pycharm,考虑以下列表,我使用以下值调用innerlist: [66957039892995'、'-70'、'-70'、'-70'、'-70'、'1'、'0'、] 我正试图用上面的列表编一本词典。当我在Python解释器中手动执行此操作时,它运行良好 datadict = { 'a': innerlist[0], 'b': innerlist[1], 'c': inne

考虑以下列表,我使用以下值调用innerlist:

[66957039892995'、'-70'、'-70'、'-70'、'-70'、'1'、'0'、]

我正试图用上面的列表编一本词典。当我在Python解释器中手动执行此操作时,它运行良好

datadict = {         
              'a': innerlist[0],         
              'b': innerlist[1],         
              'c': innerlist[2],         
              'd': innerlist[3],
              'e': innerlist[4],
              'f': innerlist[5],
              'g': innerlist[6],
              'h': "not available" if not innerlist[7] else innerlist[7]     
           }
在解释器中,它使字典没有任何错误。当我知道列表的长度时,它会输出8

但是,当我尝试在脚本中运行此代码时,Pycharm会抛出以下错误:

File "**hidden filepath**", line 25, in make_wd
    'h': "not available" if not innerlist[7] else innerlist[7]
IndexError: list index out of range
编辑:脚本:

import pymongo
from pymongo import MongoClient

def make_wd(innerlist):
    datadict = {
        'a': innerlist[0],
        'b': innerlist[1],
        'c': innerlist[2],
        'd': innerlist[3],
        'e': innerlist[4],
        'f': innerlist[5],
        'g': innerlist[6],
        'h': "not available" if not innerlist[7] else innerlist[7]
    }
    return datadict

if __name__ == '__main__':
    try:
        client = MongoClient()
    except :
        print("Could not connect to MongoDB")
    db = client.testdb
    innerlist = ['66957039892995', '-70', '-70', '-70', '1', '1', '0', '']
    testdict = make_wd(innerlist)
    db.testcollection.insert_one(testdict)

在运行脚本时,使用ipdb放置断点并检查列表中的实际内容

您可以通过安装pip安装ipdb来完成

并尝试在创建dict之前放置断点

import ipdb;ipdb.set_trace()
对于没有第7个索引的情况,应尝试使用

if len(your_list)>7 and your_list[7]:
    data = your_list[7]
else:
    data = "Not Available"

并使用数据分配h

我认为脚本中的列表缺少其中一个重复值。疏忽?您发布的代码在解释器和脚本中都可以正常工作。因此,脚本中的代码必须不同才能引发错误。@MosesKoledoye,这与我传递给脚本中函数的列表相同。请发布脚本的全部内容,以便我们能够识别错误。您的代码在解释器和脚本上都能正常工作。@Benjie太好了。他可以用来调试