Python Forum
Variable for the value element in the index function??
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Variable for the value element in the index function??
#8
'''
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)
Reply


Messages In This Thread
RE: Variable for the value element in the index function?? - by Learner1 - Jan-20-2024, 09:18 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 686 Nov-23-2023, 02:53 PM
Last Post: rob101
  Printing the variable from defined function jws 7 1,524 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  Function parameter not writing to variable Karp 5 1,067 Aug-07-2023, 05:58 PM
Last Post: Karp
  Index Function not recognized in Python 3 Peter_B_23 1 1,389 Jan-08-2023, 04:52 AM
Last Post: deanhystad
  Retrieve variable from function labgoggles 2 1,102 Jul-01-2022, 07:23 PM
Last Post: labgoggles
  Cant transfer a variable onto another function KEIKAS 5 1,981 Feb-09-2022, 10:17 PM
Last Post: deanhystad
  myList.insert(index, element) question ChrisF 1 1,696 Aug-27-2021, 03:49 PM
Last Post: bowlofred
  How i can add elements to table index of element blazej2533 3 2,078 Dec-03-2020, 08:16 PM
Last Post: Larz60+
Question Matching variable to a list index Gilush 17 6,102 Nov-30-2020, 01:06 AM
Last Post: Larz60+
  How to get index of minimum element between 3 & 8 in list Mekala 2 2,601 Nov-10-2020, 12:56 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020