如何在我的Python程序中修复`IndexError:列表索引超出范围'

时间:2019-06-01 17:00:11

标签: python

我正在尝试制作一种编程语言,但是运行它时出现错误IndexError: list index out of range,错误发生在cw = (L[words])上,准备进行连接。我该如何解决?

我尝试过join()StringIOstr+str.concat.append

def echo(filename):
    try:
        with open(filename,'r') as myFile:
            for line in myFile:
                sentence = line
                L = sentence.split()
                t = L
                words = 1
                wc = len(sentence.split())
                wc3 = float(wc)
                output = ''
                cw = ''
                for i in range(0, wc):
                    cw = (L[words])
                    output.join(cw)
                    words=words+1
                    if L[0]=='echo':
                        print(output)

0 个答案:

没有答案