"list index out of range" exception (Python3)
<p>I keep getting a list index out of range exception when I check the length of the list a. The error pops up for either the <code>if</code> or <code>elif</code> part of the second <code>if</code> statement, depending on what the user inputs. I know that when the user input is split the list is created correctly because I print it out... So I'm a little lost about why I'm getting that error.</p>

<pre><code>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.")
</code></pre>

<p>The first method is: (The methods it calls in the if statement just print things out at the moment)</p>

<pre><code>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.")
</code></pre>

<p>Other methods:</p>

<pre><code>    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)
</code></pre>

<p>The problem seems to be the definition of <code>first()</code>.  You invoke it as a function:</p>

<pre><code>if (len(a) == 2) == True: first(a)
elif (len(a) == 3) == True: first(a)
</code></pre>

<p>But you define it as a method:</p>

<pre><code>def first(self, a = list()):
</code></pre>

<p>The array of command and argument gets put into <code>self</code> and <code>a</code> is always an empty list which you attempt to index and fail.  Also, you shouldn't use a mutable type like <code>list()</code> as a default value unless you're certain what you are doing.  I suggest simply:</p>

<pre><code>def first(a):
</code></pre>

<p>As far as your <code>__main__</code> code goes, simplify:</p>

<pre><code>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 &lt;= length &lt;= 3:
            first(tokens)
        else:
            print("Invalid Input. Please Re-enter.")
</code></pre>

python list