Untitled
unknown
plain_text
2 years ago
2.2 kB
7
Indexable
# Write a python 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
# else:
# simply reverse the string
# Decoding:
# if the word contains less than 3 characters, reverse it
# else:
# remove 3 random characters from start and end. Now remove the last letter and append it to the beginning
# Your program should ask whether you want to code or decode
#message = input ("Enter a message to code and decode :")
# Write a python 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
# else:
# simply reverse the string
# Decoding:
# if the word contains less than 3 characters, reverse it
# else:
# remove 3 random characters from start and end. Now remove the last letter and append it to the beginning
# Your program should ask whether you want to code or decode
import random
#import string
# Define the sequence of characters (in this case, lowercase letters)
def code (message):
mylist = 'abcdefghijklmnopqrstuvwxyz'
rd1 ="".join(random.choice(mylist) for _ in range(3))
rd2 ="".join(random.choice(mylist) for _ in range(3))
for word in message.split(" "):
if len(word) >= 3:
word = word[1:] + word[0]
word = rd1 + word + rd2
print(word,end = " ")
else:
word =word[::-1]
print(word, end =" ")
def decode (message):
for word in message.split(" "):
if len(word) < 3:
word = word[::-1]
print(word)
else :
word = word[3:-3]
word = word[-1] + word[:-1]
print(message)
message = input("Enter your message: ")
action = input("1 for code / 2 for decode :")
if action == "1":
code(message)
elif action == "2":
decode(message)
else :
print("Invalid output")Editor is loading...
Leave a Comment