"列出索引超出范围"异常(Python3)

时间:2016-09-11 04:33:43

标签: python list

当我检查列表的长度时,我不断得到列表索引超出范围异常。第二个if语句的elifif部分会弹出错误,具体取决于用户输入的内容。我知道当用户输入被拆分时,列表会被正确创建,因为我打印出来了......所以我有点迷失了为什么我会收到这个错误。

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)

2 个答案:

答案 0 :(得分:2)

问题似乎是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.")

答案 1 :(得分:0)

真实问题:

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

def first(a=list())

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

我可以指出的第二个问题是,您试图在字符串函数之间使用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