Created with 42pag.es | edit →

# Caesar Cipher - July 2017

# Encoding a message

# Initialise the variables

lowerCase="abcdefghijklmnopqrstuvwxyz"

offset=0

ciphertext=""

# get the message from the user

message=input("Enter message to encode: ")

# force the message to be all lower case letters

message=message.lower()

# get the offset from the user, only accept 1 - 25

while offset<1 or offset>25:

offset=int(input("Enter the offset (1 - 25): "))

# split the message into individual characters

for char in message:

# check if the character is a lower case letter

if char in lowerCase:

#Work out the position of the letter (0-25)

position=lowerCase.index(char)

# Work out the new encoded position

newPosition=(position+offset) % 26

# Get encoded letter from new position

codedLetter=lowerCase[newPosition]

# Add the encoded letter to the others already done

ciphertext=ciphertext+codedLetter

else:

# Add the character without any encoding

ciphertext=ciphertext+char

print("Encoded message is:             ",ciphertext)