# Functions def int_check(question): """Checks users enter an integer.""" error = "Please enter an integer between 0 and 26." while True: try: # Return the response if it's an integer response = int(input(question)) if 0 < response < 26: return response else: print(error) except ValueError: print(error) def string_check(question, valid_ans_list): """Checks that users enter the full word or the first letter of a word from a list of valid responses""" result = '' while True: response = input(question).lower() for item in valid_ans_list: # check if the response is the entire word if response == item: return item # check if it's the first letter elif response == item[:1]: return item print(f"Please choose an option from {valid_ans_list}") def encrypt_func(): '''Encrypts user input by custom amount''' result = '' plain_text = input("Plain text: ") shift = int_check("Shift amount: ") for char in plain_text: if char.isupper(): FIRST_CHAR_CODE = 65 LAST_CHAR_CODE = 90 if char.islower(): FIRST_CHAR_CODE = 97 LAST_CHAR_CODE = 122 if char.isalpha(): char_code = ord(char) new_char_code = char_code + shift if new_char_code > LAST_CHAR_CODE: new_char_code -= CHAR_RANGE if new_char_code < FIRST_CHAR_CODE: new_char_code += CHAR_RANGE new_char = chr(new_char_code) result += new_char else: result += char print("Encrypted Text: " + result) print() def decrypt_func(): '''Gives all possible deciphered texts''' cipher_text = input("Cipher Text: ") shift = 1 result = "" while shift < 25: for char in cipher_text: if char.isupper(): FIRST_CHAR_CODE = 65 LAST_CHAR_CODE = 90 if char.islower(): FIRST_CHAR_CODE = 97 LAST_CHAR_CODE = 122 if char.isalpha(): char_code = ord(char) new_char_code = char_code + shift if new_char_code > LAST_CHAR_CODE: new_char_code -= CHAR_RANGE if new_char_code < FIRST_CHAR_CODE: new_char_code += CHAR_RANGE new_char = chr(new_char_code) result += new_char else: result += char shift + 1 # Main Routine print() print("=== Caesar Cipher Encrypter & Decrypter ===") print() CHAR_RANGE = 26 mode = string_check("Encrypt or Decrypt? ", ['encrypt', 'decrypt']) if mode == "encrypt": encrypt_func() else: decrypt_func()