Python Flip Cipher
<p>For a project, we have to make a program where one inputs a keyword and a string and the program encodes it. For instance</p>

<p>String: But I didn't think.
Keyword: Wonderland</p>

<p>The program would split up 'Wonderland' into its position in the alphabet:</p>

<pre><code>WONDERLAND = 23,15,14,4,5,18,12,1,14,4
</code></pre>

<p>Then it would swap the character in position 1 with the character in position 23, the character in position 2 with the character in position 15, etc.</p>

<p>'But I didn't think' would come out as "'nu  n. ithdtBiItdk" </p>

<p>I have been able to encode a string successfully, but not decode it. Here is my code: </p>

<pre><code>def encode(keyword,t):
    key = []
    text = []

    for i in range(len(t)):
        text.append(t[i])

    for i in range(len(keyword)):
        key.append(ord(keyword[i].lower()) - 96)

    keynum = 0
    actnum = 0

    for i in range(len(text)):
        print str(i) + " " + str(text)

        if keynum &gt; len(keyword) - 1:
            keynum = 0

        actnum = key[keynum]

        while (actnum + i) &gt; len(text) - 1:
            actnum = actnum - len(text)

        temp = text[i]
        text[i] = text[i+actnum]
        text[i+actnum] = temp
        keynum += 1

    merged = ""

    for i in range(len(text)):
        merged += text[i]

    return merged

def decode(keyword,t):
    key = []
    text = []

    for i in range(len(t)):
        text.append(t[i])

    for i in range(len(keyword)):
        key.append(ord(keyword[i].lower()) - 96)

    keynum = 0
    actnum = 0
    total = len(keyword)

    for i in reversed(range(len(text))):
        print str(i) + " " + str(text)

        keynum = total

        while keynum &gt; len(keyword) - 1:
            keynum -= len(keyword)
            keynum += 1

        if keynum &lt; 0:
            keynum = len(keyword) - 1

        actnum = key[keynum]

        while (actnum + i) &gt; len(text) - 1:
            actnum = actnum - len(text)

        temp = text[i]
        text[i] = text[i+actnum]
        text[i+actnum] = temp
        total -= 1

    merged = ""

    for i in range(len(text)):
        merged += text[i]

    return merged

x = encode("wonderland","But I didn't think.")
print decode("wonderland",x)
</code></pre>

<p><strong>EDIT:</strong> The program is set up so that if the character specified doesn't exist, it takes the character it was supposed to be and subtracts the length of the string from it until it is inside the string.</p>

<p><strong>EDIT:</strong> Basically, if you could just help me reverse the 'encode' function, that would be great.</p>
