import string import pandas # Functions go here def make_statement(statement, decoration): """Emphasises headings by adding decoration at the start and end""" return f"{decoration * 3} {statement} {decoration * 3}" def string_check(question, valid_answers=('yes', 'no'), num_letters=1): """Checks that users enter the full word or the 'n' letters of a word from a list of valid responses""" while True: response = input(question).lower() for item in valid_answers: # check if the response is the entire word if response == item: return item # check if it's the first letter elif response == item[:num_letters]: return item print(f"Please choose an option from {valid_answers}") def not_blank(question): """Checks that a user response is not blank""" while True: response = input(question) if response != "": return response print("Sorry, this can't be blank. Please try again.\n") def instructions(): make_statement("Instructions", "🎲") print('''Thank you for using this decoding program! First, select whether you want to encode or decode. If you want to disguise a message using the Caesar Cipher, select encode. If you have an already encoded message that you want to understand, select decode. Then enter your message and the number of letters you want it to be shifted by. Please do not attempt to copy and paste long messages into the program as they may end the dialogue. Instead, try decoding paragraph by paragraph. When you are done using the program, type 'xxx' into the enter message section. The program will record the messages you have encoded/decoded, what they were shifted by and how many times you have used the program. Once you have entered the exit code ('xxx'), the program will display your user history and write the data as a text file.''') def encode(message_to_encode): encoded_message = "" for letter in message_to_encode: if letter in alphabet: new_position = alphabet.index(letter) + shift_number new_position = new_position % len(alphabet) encoded_message = encoded_message + alphabet[new_position] elif letter in upper_alphabet: new_position = upper_alphabet.index(letter) + shift_number new_position = new_position % len(upper_alphabet) encoded_message = encoded_message + upper_alphabet[new_position] else: encoded_message = encoded_message + letter print("This is your encoded message:", encoded_message) return encoded_message def decode(message_to_decode): decoded_message = "" for letter in message_to_decode: if letter in alphabet: new_position = alphabet.index(letter) - shift_number new_position = new_position % len(alphabet) decoded_message = decoded_message + alphabet[new_position] elif letter in upper_alphabet: new_position = upper_alphabet.index(letter) - shift_number new_position = new_position % len(upper_alphabet) decoded_message = decoded_message + upper_alphabet[new_position] else: decoded_message = decoded_message + letter print("This is your decoded message:", decoded_message) return decoded_message # initialise alphabet + enc_dec alphabet = list(string.ascii_lowercase) upper_alphabet = list(string.ascii_uppercase) messages_encoded = 0 messages_decoded = 0 cipher_types = ('encode', 'decode', 'xxxxx') # lists to hold message information all_messages = [] all_shift_numbers = [] all_decoded_messages = [] all_encoded_messages = [] caesar_cipher_dict = { 'Message': all_messages, 'Shift Number': all_shift_numbers, } # Main routine goes here make_statement("Caesar Cipher Program", "⚖") print() want_instructions = string_check("Do you want to see the instructions? ") if want_instructions == "yes": instructions() print() while True: # ask user for their message (and check it's not blank) print() message = not_blank("What is your message? ") # if name is exit code, break out of loop if message == "xxx": break # Ask for their shift_number shift_number = int(input("How many letters would you like to shift your message by? ")) # ask user for cipher type (encode / decode / enc / dec) cipher_type = string_check("Type of cipher (encode/decode): ", cipher_types, 3) if cipher_type == 'encode': returned_encoded_message = encode(message) messages_encoded += 1 if cipher_type == 'decode': returned_decoded_message = decode(message) messages_decoded += 1 # add message list, shift numbers, decoded and encoded messages all_messages.append(message) all_shift_numbers.append(shift_number) all_decoded_messages.append(returned_decoded_message) # End of CC Loop total_program_use = messages_encoded + messages_decoded # print practice funct print(f"Your messages were: {all_messages}") print(f"You shifted these messages by: {all_shift_numbers}") print(f"You encoded {messages_encoded} messages") print(f"You decoded {messages_decoded} messages") print(f"You used the program {total_program_use} times")