I am pretty new to Python and am trying to create an encryption method inspired from ROT13.
I kind of finished the code but I want to use it in Linux terminal with arguments such as -e
- ( I want -e
to stand for "encrypt" and -h
which obviously stands for help.)
I've done some googling and found out about importing "argparse", I tried something like this :
import argparse
parser =argparse.ArgumentParser(description='An
encryption method inspired from Rot13.')
args = parser.parse_args()
And it didn't work.
Any reading material or code fixes would be really helpful.
Here is my python code:
import argparse
parser = argparse.ArgumentParser(description='An encryption method inspired from Rot13.')
args = parser.parse_args()
import sys
# Dictionary to lookup the index of alphabets
alphabet = {'A' : 1, 'B' : 2, 'C' : 3, 'D' : 4, 'E' : 5,
'F' : 6, 'G' : 7, 'H' : 8, 'I' : 9, 'J' : 10,
'K' : 11, 'L' : 12, 'M' : 13, 'N' : 14, 'O' : 15,
'P' : 16, 'Q' : 17, 'R' : 18, 'S' : 19, 'T' : 20,
'U' : 21, 'V' : 22, 'W' : 23, 'X' : 24, 'Y' : 25, 'Z' : 26}
# Dictionary to lookup alphabets corresponding to the index after inc
enchabet = {0 : 'Z', 1 : 'A', 2 : 'B', 3 : 'C', 4 : 'D', 5 : 'E',
6 : 'F', 7 : 'G', 8 : 'H', 9 : 'I', 10 : 'J',
11 : 'K', 12 : 'L', 13 : 'M', 14 : 'N', 15 : 'O',
16 : 'P', 17 : 'Q', 18 : 'R', 19 : 'S', 20 : 'T',
21 : 'U', 22 : 'V', 23 : 'W', 24 : 'X', 25 : 'Y'}
# Function to rotLPWen the string
# according to the inc provided
def rotLPWen(msg, inc):
calc = ''
for char in msg:
# checking for space
if(char != ' '):
# looks up the dictionary and
# adds the inc to the index
num = ( alphabet[char] + inc ) % 26
# looks up the second dictionary for
# the inced alphabets and adds them
calc += enchabet[num]
else:
# adds space
calc += ' '
#print("You can only type one word at a time!")
#sys.exit(1)
return calc
# Function to rotLPWde the string
# according to the inc provided
def rotLPWde(msg, inc):
calced = ''
for char in msg:
# checks for space
if(char != ' '):
# looks up the dictionary and
# subtracts the inc to the index
num = ( alphabet[char] - inc + 26) % 26
# looks up the second dictionary for the
# inced alphabets and adds them
calced += enchabet[num]
else:
# adds space
calced += ' '
#print("You can only type one word at a time!")
#sys.exit(1)
return calced
# driver function to run the program
def main():
# use 'upper()' function to convert any lowercase characters to uppercase
msg = input("Plase type something!")
msg = msg.strip()
if ' ' in msg.strip():
words = msg.split(' ')
encrypted = ''
increments = []
for word in words:
# Create an increment for the number of characters in the sentence
increment = len(word)
inc = increment
result = rotLPWen(word.upper(), inc)
encrypted += result + ' '
increments.append(inc)
else:
increments = len(msg)
inc = increments
encrypted = rotLPWen(msg.upper(), inc)
#Creates a list from the sentence
#list1 = list(map(len,msg.split()))
print(encrypted)
print(increments)
# Executes the main function
if __name__ == '__main__':
main()