I need to write a code that shifts each letter of a string (cipher) a given number (shift) further alphabetically. The code I have written so far is as follows (with an example to test the function):
def decode(cipher, shift):
letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
for letter in cipher:
index = letters.index(letter)
cipher = cipher.replace(letter, letters[index-shift])
return cipher
print decode("LIPPSASVPH", 4)
This almost works, except what happens as the code goes through the cipher, it will change any letter that matches the one it is changing now, so for my example it should return “HELLOWORLD”, but “DELLOWORLD is returned instead” "because the last letter of the encryption is H, and the code previously changed L at the beginning of the encryption to H, so the code changes both values of H to D. Any suggestions on how to make the code run on each letter separately without changing the other letters? Thank you all very much.