Assignment 1

 avatar
unknown
python
2 years ago
1.8 kB
4
Indexable
# Coursework Assessment 1
# Name: Leigh Clarke
# Student No: 2245151

# A Caesar Cipher Program
import os.path

def welcome():
    print(" Welcome to the Caesar Cipher\n This program encrypts and decrypts text with the Caesar Cipher.")
    return


def enter_message():
    mode = ''
    message = ''
    shift = 0
    while True:
        mode = input('Would you like to encrypt (e) or deccrypt (d): ')
        if mode =='e' or mode =='d':
            message = input('What message would you like to encode: ')
            while True:
                try:
                    shift = int(input('What is the shift number: '))
                except ValueError:
                    print('Invalid Shift')
                else:
                    if 1 <= shift <= 25:
                        break
                    else:
                        print('Invalid Shift')
            return (mode, message, shift)
        print('Invalid Mode')


def encrypt(message, shift):
    alphabet = 'abcdefghijklmnopqrstuvwxyz'
    encrypted_output = ''
    i = ''
    for ch in message:
        if ch in alphabet:
            i = alphabet.index(ch)
            j = (i + shift) % 26
            encrypted_output = encrypted_output + alphabet[j]
        else:
            encrypted_output = encrypted_output + ch
    return(encrypted_output)


def decrypt(message,shift):
    alphabet = 'abcdefghijklmnopqrstuvwxyz'
    deencrypted_output = ''
    for ch in message:
        if ch in alphabet:
            i = alphabet.index(ch)
            j = (26 + i - shift) % 26
            deencrypted_output = deencrypted_output + alphabet[j]
        else:
            deencrypted_output = deencrypted_output + ch
    return(deencrypted_output)
Editor is loading...