0

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()
Red
  • 26,798
  • 7
  • 36
  • 58
  • Where exactly are you using the parsed `args`. You have to check if the args is valid and then get the `encrypted` status. I suggest you look through [this tutorial](https://realpython.com/command-line-interfaces-python-argparse/#how-to-use-the-python-argparse-library-to-create-a-command-line-interface) – Ceres Mar 18 '21 at 16:15
  • I didn't try the encryption part yet, but ./rotLPW.py -h won't work, unless I use " python3 rotLPW.py -h", work is there any way to make this work in both ways. Also, thank you for the tutorial! – BillyBonky Mar 18 '21 at 16:24
  • Making a script run without putting `python` before the script name is a completely unrelated issue. – Barmar Mar 18 '21 at 16:34
  • 1
    See https://stackoverflow.com/questions/4993621/how-to-run-python-script-without-typing-python for that – Barmar Mar 18 '21 at 16:35

0 Answers0