String index not working? - Python
<p>I'm Making a sort of cipher that requires a string to be read backwards and i get an indexing error even though the index I reference is well within range:</p>

<pre><code>M = str(input("Input Message: "))
M = M.upper()
L = len(M)
A = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]

def DECRYPT():
    global L
    global M
    global A

    if L%2 != 0:
        POS = False
    else:
        POS = True

    i = L-1
    NM = ""
    while 1:
        if M[i] != " ":
            INDEX = A.index(M[i])
            if POS == True:
                INDEX += (i + 1)
            else:
                INDEX -= (i)
            INDEX %= 26        
            NM = NM+A[INDEX]
            i += 1
            if POS == True:
                POS = False
            else:
                POS = True
    print("\n"+NM)

def ENCRYPT():
    global L
    global A
    global M

    POS = True
    M = M[::-1]
    i = 0
    NM = ""

    while 1:
        if i == L:
            break
        if M[i] != " ":
            INDEX = A.index(M[i])
            if POS == True:
                INDEX += (i + 1)
            else:
                INDEX -= (i + 1)
            INDEX %= 26        
            NM = NM+A[INDEX]
        i += 1
        if POS == True:
            POS = False
        else:
            POS = True
    print("\n"+NM)

while 1:
    C = int(input("\nWhat do you want to do:\n1) Encrypt Something\n2)Decrypt Something\n\n"))
    if C == 1:
            ENCRYPT()
    if C == 2:
            DECRYPT()
</code></pre>

<p>where i is a placeholder value. I run it and get this:</p>

<pre><code>Input Message: ABC

What do you want to do:
1) Encrypt Something
2)Decrypt Something

2
Traceback (most recent call last):
  File "C:\Users\Danny\Google Drive\SHIFT.py", line 67, in &lt;module&gt;
    DECRYPT()
  File "C:\Users\Danny\Google Drive\SHIFT.py", line 19, in DECRYPT
    if M[i] != " ":
IndexError: string index out of range
</code></pre>

<p>I have tried changing value of i to no avail.</p>