#Rail fence cipher (2 rails)

def rail_fence_encode(plainText):
    evenCharacters = ''
    oddCharacters = ''
    charCount = 0
    for character in plainText:
        if charCount % 2 is 0:
            evenCharacters = evenCharacters + character
        else:
            oddCharacters = oddCharacters + character
        charCount += 1
    cipherText = oddCharacters + evenCharacters
    return cipherText



def rail_fence_decode(cipherText):
    halfLength = len(cipherText) // 2
    oddCharacters = cipherText[:halfLength]
    evenCharacters = cipherText[halfLength:]
    plainText = ''
    
    for index in range(halfLength):
        plainText = plainText + evenCharacters[index]
        plainText = plainText + oddCharacters[index]

    if len(evenCharacters) > len(oddCharacters):
        plainText = plainText + evenCharacters[-1]

    return plainText

  
#Main program
print('RAIL FENCE')
print('\nOPTIONS')
print('')
flag = True
while flag:
    print('\n1. Encrypt a message')
    print('2. Decrypt a message')
    print('3. Exit the program')
    answer = input("Please select an option. ")
    if answer == '1':
        message = input('Enter the message you want to encrypt: ')
        print('\nThe encoded message is: ')
        print(rail_fence_decode(message))
    elif answer == '2':
        message = input('Enter the message you want to decrypt: ')
        print('\nThe decoded message is: '),
        print(rail_fence_encode(message))
    elif answer =='3':
        flag = False
print('Good bye')

