10.Secret Encoding & Decoding.py

# Write a program to translate a message into secret code language.
# Use the rules below to translate normal English into secret code language.


# Coding:
# if the word contains atleast 3 characters:
# remove the first letter and append it at the end now append three random characters at the starting and the end
# now append three random characters at the starting and the end
# else:
# simply reverse the string

# Decoding:
#if the word contains less than 3 characters:
# reserve it
#else:
# remove 3 random characters from start and end. Now remove the last letter append it to the beginning


import random

# Function to generate a random prefix
def generate_prefix():
prefixes = ['asd', 'jhg', 'iuy', 'ert', 'bnm']
return random.choice(prefixes)
def generate_postfix():
postfixes = ['asd', 'jhg', 'iuy', 'ert', 'bnm']
return random.choice(postfixes)

choice = int(input("Enter your choice: for decoding press 1 and for encoding press 2: "))
message =input("Enter your message:")
words = message.split(" ")

match choice:
case 1:
# coding = False
# if coding:
nwords = []
for word in words:
if len(word) >= 3:
# Remove the first letter and append it at the end
modified_word = word[1:] + word[0]
# Append three random characters at the starting and the end
modified_word = generate_prefix() + modified_word + generate_postfix()
nwords.append(modified_word)
else:
# Simply reverse the string
nwords.append(word[::-1])
print("Encoded Message is:"," ".join(nwords))

case 2:
# else:
nwords = []
for word in words:
if len(word) >= 3:
# Remove three random characters at the starting and the end
modified_word = word[3:-3]
# Remove the last letter and append it at the beginning
modified_word = modified_word[-1] + modified_word[:-1]
nwords.append(modified_word)
else:
# Simply reverse the string
nwords.append(word[::-1])
print("Decoded Message is:"," ".join(nwords))

case _:
print("Wrong Choice Entered!!!")





 

Comments

Popular posts from this blog

3.FaulyCalculator.py

11.Snake Water Gun Game.py

15.PyStopWatch.py