I have just written a simple program to implement the Caesar cipher using lists. The program assumes the input will be all capital letters. I'm quite new to Python and am just curious of some more efficient ways to write this code. Honestly I just want to work on defining functions and all that jazz.
print("This encryption program uses the Caesar cipher to encrypt your message.")
mode = input("E to Encrypt or D to Decrypt")
if mode == "E":
string = input("Input your message to encrypt in all capital letters: ")
key = int(input("Create your encryption key (remember this)"))
length = len(string)
old_values = [0]*length
new_values = [0]*length
olds_position = 0
news_position = 0
for char in string:
old_values[olds_position] = ord(char)
olds_position += 1
for value in old_values:
if value != 32:
new_values[news_position] = (value + key)
else:
new_values[news_position] = value
news_position += 1
for newvalue in new_values:
if newvalue != 32:
if newvalue > ord('Z'):
newvalue -= 26
elif newvalue < ord('A'):
newvalue += 26
print(chr(newvalue)+"", end="")
else:
print(" ", end="")
if mode == "D":
key = int(input("Enter your encryption key: "))
string = input("Input your encrypted message to descrypt it: ")
length = len(string)
old_values = [0]*length
new_values = [0]*length
olds_position = 0
news_position = 0
for char in string:
old_values[olds_position] = ord(char)
olds_position += 1
for value in old_values:
if value != 32:
new_values[news_position] = (value - key)
else:
new_values[news_position] = value
news_position += 1
for newvalue in new_values:
if newvalue != 32:
if newvalue > ord('Z'):
newvalue -= 26
elif newvalue < ord('A'):
newvalue += 26
print(chr(newvalue)+"", end="")
else:
print(" ", end="")