当我在文本文件中输入“tests are fun”并从中读取以加密它时,我使用python作为键,得到“ICLAGQPPXKTHCB”而不是“ICLAGNGCYBB”。是什么导致了这个问题?当我尝试在转换字符串后添加if语句时,我不断收到错误,指出file1未定义
CODE:
Alphabet = ["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", ",", ".", "-", " ","\n"]
*def encrypt():
count = 0
i = 0
output = []
encrypted = []
keylist = []
stringlist = []
**import os
key = input("Please enter the key you want to use: ")
fname = input("Enter filename to open and decode: ")
fname1 = fname + (".txt")
if os.path.isfile(fname1):
file1 = open(fname1,'r')
file1 = file1.read()
mod = fname + ("_e.txt")
file2 = open(mod,'w')
else:
print()
print("file doesnt exist try again")
return()
for index, letter in enumerate(file1):
if letter.isalpha():
number = alphabet.index(letter)
cw = key[index % len(key)]
shift = alphabet.index(cw)
nli = (number + shift) % 26
nl = alphabet[nli]
output += nl
else:
output += letter
# main function
if __name__ == '__main__':
main()*