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
当我试图对一个大写和小写字母的单词进行加密时,它不起作用,我试图插入一个命令或其他东西来做我的不区分大小写的
取决于您想要如何处理它。您可以使用.lower()
将所有内容转换为小写并使用它。但是,如果你想保留大小写信息,你可以将每个字符转换为小写,但要跟踪它是否为大写,然后在写作时,你可以转换回大写。类似这样的东西:
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)
编辑:
它在您的代码中实现:我允许自己简化它的某些部分,检查它是否仍能达到你的预期。
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