Jan-20-2024, 09:18 PM
'''
This program is designed to make a secret message encoder for my kids to try out,
but also, to help me learn Python, and to try out the "for loop" function in combination
with other Python commands. As I write this code, I have only been using Python
for two weeks, and I have written less than 20 Python files so far. 01/20/2023
'''
user_message = input("Enter a secret message to be put in cipher: ")
alphabet_list = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
"m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x",
"y", "z"]
numbers_to_text = {
"1": "one",
"2": "two",
"3": "three",
"4": "four",
"5": "five",
"6": "six",
"7": "seven",
"8": "eight",
"9": "nine",
"0": "zero",
}
secret_msg = ""
test = False
while test == False:
secret_number = input("Enter a secret whole number between 1 and 26: ")
if secret_number.isnumeric():
secret_number = int(secret_number)
if secret_number > 0 and secret_number < 27:
test = True
else:
print("Not a whole number from 1-26")
else:
print("Not a numeric value")
import string
# The next two lines convert any numbers in the sentence to their text equivalents
num_table = str.maketrans(numbers_to_text)
num_to_text = user_message.translate(num_table)
# The next line makes everything lower case
lower_msg = num_to_text.lower()
# The next 2 lines remove any punctuation
translator = str.maketrans("", "", string.punctuation)
no_punct = lower_msg.translate(translator)
# The next line removes all the original word spacing
clr_spaces = no_punct.replace(" ", "")
lower_msg = clr_spaces
string_length = len(lower_msg)
count = 0
# This is my very first use of a for loop function
for letter in lower_msg:
alpha_char = lower_msg[count]
# print(alpha_char) This line was just added as a debugging test
count += 1
index_num = alphabet_list.index(alpha_char)
new_index = index_num - secret_number
new_char = alphabet_list[new_index]
secret_msg = secret_msg + new_char
# The next two lines add a space every five characters to hide original word spacing
import re
chunked = (" ").join(re.findall(".{1,5}", secret_msg))
secret_msg = chunked
print("\nYour secret message now reads: " + secret_msg)
This program is designed to make a secret message encoder for my kids to try out,
but also, to help me learn Python, and to try out the "for loop" function in combination
with other Python commands. As I write this code, I have only been using Python
for two weeks, and I have written less than 20 Python files so far. 01/20/2023
'''
user_message = input("Enter a secret message to be put in cipher: ")
alphabet_list = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
"m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x",
"y", "z"]
numbers_to_text = {
"1": "one",
"2": "two",
"3": "three",
"4": "four",
"5": "five",
"6": "six",
"7": "seven",
"8": "eight",
"9": "nine",
"0": "zero",
}
secret_msg = ""
test = False
while test == False:
secret_number = input("Enter a secret whole number between 1 and 26: ")
if secret_number.isnumeric():
secret_number = int(secret_number)
if secret_number > 0 and secret_number < 27:
test = True
else:
print("Not a whole number from 1-26")
else:
print("Not a numeric value")
import string
# The next two lines convert any numbers in the sentence to their text equivalents
num_table = str.maketrans(numbers_to_text)
num_to_text = user_message.translate(num_table)
# The next line makes everything lower case
lower_msg = num_to_text.lower()
# The next 2 lines remove any punctuation
translator = str.maketrans("", "", string.punctuation)
no_punct = lower_msg.translate(translator)
# The next line removes all the original word spacing
clr_spaces = no_punct.replace(" ", "")
lower_msg = clr_spaces
string_length = len(lower_msg)
count = 0
# This is my very first use of a for loop function
for letter in lower_msg:
alpha_char = lower_msg[count]
# print(alpha_char) This line was just added as a debugging test
count += 1
index_num = alphabet_list.index(alpha_char)
new_index = index_num - secret_number
new_char = alphabet_list[new_index]
secret_msg = secret_msg + new_char
# The next two lines add a space every five characters to hide original word spacing
import re
chunked = (" ").join(re.findall(".{1,5}", secret_msg))
secret_msg = chunked
print("\nYour secret message now reads: " + secret_msg)