Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
267 views
in Technique[技术] by (71.8m points)

I have a code that i wrote in python 3 for encryption and i manage to do the encryption. But i have to do it case insensitive and i cannot

import sys
# Here are the arg that we are going to import from command line to be able to use them.
arg1 = str(sys.argv[1]) # incoming command line arguments
arg2 = str(sys.argv[2])
arg3 = str(sys.argv[3])
arg4 = str(sys.argv[4]) 
arg5 = str(sys.argv[5]) 
arg6 = str(sys.argv[6]) 
arg7 = str(sys.argv[7]) 
arg8 = str(sys.argv[8]) 

space = ' '
upper =['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']
lower =['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']

if(arg8 == "enc"):
    key = int(arg6) # the key to be use for encryption
    file1 = open(arg4, "a") 
    with open(arg2, 'r') as r: 
        for line in r:
           for i in range(0,len(line)):
               if(line[i] != space):
                   for j in range(0,26): 
                       if(line[i] == lower[j]): #compare so i can encrytion my letters
                           if((j+int(key))>25): 
                               file1.write(lower[((j+int(key))%25)-1]) 
                           else:
                               file1.write(lower[j+int(key)]) 
               elif(line[i] == space):
                   file1.write(space)
           print()
    file1.close() # We need to closed the file

When i am trying to do encryption for a word with upper and lower letter is not working and i am trying to insert a command or something to do my code case insensitive


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Depends how you want to handel it. You could just convert everything to lower case with .lower() and work with that. But if you want to keep the case information, you could convert each character to lower case but keep track if it was upper and then when it comes to writing, you can convert back to upper. Something like this:

c = "A" # The character we are working on
if c.isupper():
  upper = True
  c = c.lower() # Now c is always lower case
else:
  upper = False

# Do the encryption on `c`...
# 

if upper:
   file.write(c.upper()) # Input was upper case, so save it as such
else:
   file.write(c)

Edit:

Here it is implemented in your code: I allowed myself to simplify some parts of it, check if it still does what you were expecting.

import sys
# Here are the arg that we are going to import from command line to be able to use them.
arg1 = str(sys.argv[1]) # incoming command line arguments
arg2 = str(sys.argv[2])
arg3 = str(sys.argv[3])
arg4 = str(sys.argv[4]) 
arg5 = str(sys.argv[5]) 
arg6 = str(sys.argv[6]) 
arg7 = str(sys.argv[7]) 
arg8 = str(sys.argv[8]) 

space = ' '
lower =['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']

if(arg8 == "enc"):
    key = int(arg6) # the key to be use for encryption
    file1 = open(arg4, "a") 
    with open(arg2, 'r') as r: 
        for line in r:
            for c in line: # for each character `c` in `line`
                if c.isupper():
                    upper = True
                    c = c.lower() # Now c is always lower case
                else:
                    upper = False

                # If `c` is in the `lower` array
                if(c in lower):
                    j = lower.index(c) # Get index of c in lower
                    
                    if((j+int(key))>25): 
                        output = lower[((j+int(key))%25)-1]
                    else:
                        output = lower[j+int(key)]
                    
                    # Change Output back to uppwer if input was upper
                    if upper:
                        output = output.upper()
                    file1.write(output)
                elif(c == space):
                    file1.write(space)
                ## Warning! All other characters such as .,+- will be ignored!
            print()
    file1.close() # We need to closed the file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...