"list index out of range"异常 (Python3)



当我检查列表a的长度时,我一直得到列表索引超出范围异常。第二个if语句的ifelif部分弹出错误,这取决于用户输入的内容。我知道当用户输入被分割时,列表是正确创建的,因为我把它打印出来了……我不太清楚为什么会出现这个错误

if __name__ == '__main__':
            for line in sys.stdin:
                    s = line.strip()
                    if not s: break
                    if (str(s) is "quit") == True: quit()
                    elif (str(s) is "quit") == False:
                            a = s.split()
                            print(a)
                            if (len(a) == 2) == True: first(a)
                            elif (len(a) == 3) == True: first(a)
                            else: print("Invalid Input. Please Re-enter.")

第一个方法是:(它在if语句中调用的方法只是打印当前的内容)

def first(self, a = list()):
            word = a[0]
            if word is ls:
                    ls(a[1])           
            elif word is format:
                    form(a[1])        # EDIT: was format
            elif word is reconnect:
                    reconnect(a[1])
            elif word is mkfile:
                    mkfile(a[1])
            elif word is mkdir:
                    mkdir(a[1])
            elif word is append:
                    append(a[1], a[2])                               
            elif word is delfile:
                    delfile(a[1])
            elif word is deldir:
                    deldir(a[1])
            else:
                    print("Invalid Prompt. Please Re-enter.")
其他方法:

    def reconnect(one = ""):
            print("Reconnect")
    def ls(one = ""):
            print("list")
    def mkfile(one = ""):
            print("make file")
    def mkdir(one = ""):
            print("make drive")
    def append(one = "", two = ""):
            print("append")
    def form(one = ""):
            print("format")
    def delfile(one = ""):
            print("delete file")
    def deldir(one = ""):
            print("delete directory")
    def quit():
            print("quit")
            sys.exit(0)

问题似乎是first()的定义。您可以将其作为函数调用:

if (len(a) == 2) == True: first(a)
elif (len(a) == 3) == True: first(a)

但是你把它定义为一个方法:

def first(self, a = list()):

命令和参数数组被放入selfa总是一个空列表,您尝试索引并失败。另外,除非确定要做什么,否则不应该使用像list()这样的可变类型作为默认值。我的建议很简单:

def first(a):

对于您的__main__代码,请简化:

if __name__ == '__main__':
    for line in sys.stdin:
        string = line.strip()
        if not string:
            break
        if string == "quit":
            quit()
        tokens = string.split()
        length = len(tokens)
        if 2 <= length <= 3:
            first(tokens)
        else:
            print("Invalid Input. Please Re-enter.")

实际情况:

要解决您的错误,您必须删除 first函数的self参数

def first(a=list())

基本上self仅用于面向对象创建方法。像你这样的函数不能使用self,否则你会将第一个参数传递给self,而不是传递给你想要的a。

我可以指出的第二个问题是,你正试图在字符串函数之间使用is进行比较。

 def first(a = list()):
        word = a[0]
        if word is "ls":
                ls(a[1])           
        elif word is "format":
                format(a[1])
        elif word is "reconnect":
                reconnect(a[1])
        elif word is "mkfile":
                mkfile(a[1])
        elif word is "mkdir":
                mkdir(a[1])
        elif word is "append":
                append(a[1], a[2])                               
        elif word is "delfile":
                delfile(a[1])
        elif word is "deldir":
                deldir(a[1])
        else:
                print("Invalid Prompt. Please Re-enter.")
额外

Python中内置操作的is函数。is比较对象的权益。

但是这个表达式:

if (str(s) is "quit") == True:

可以更简单,如:

if str(s) == "quit":

或:

if str(s) is "quit":

== True是没有意义的== False,你可以使用not更pythonically。

最新更新