Python 使用用户输入进行加密/解密

Python 使用用户输入进行加密/解密,python,encryption,Python,Encryption,Idk为什么没有输出到文件中,有什么想法或帮助吗 def encrypt(text, key): alphabet = "abcdefghijklmnopqrstuvwxyz" text = text.lower() cipherText = "" for ch in text: idx = alphabet.find(ch) cipherText = cipherText + key[idx] return cipherT

Idk为什么没有输出到文件中,有什么想法或帮助吗

def encrypt(text, key):
    alphabet = "abcdefghijklmnopqrstuvwxyz"
    text = text.lower()
    cipherText = ""
    for ch in text:
        idx = alphabet.find(ch)
        cipherText = cipherText + key[idx]
    return cipherText

def decrypt(cipherText, key):
    alphabet = "abcdefghijklmnopqrstuvwxyz"
    cipherText = cipherText.lower()
    text = ""
    for ch in cipherText:
        idx = key.find(ch)
        text = text + alphabet[idx]
    return text

def main():
    userInput = input("Operation (encrypt, decrypt, exit): ")
    while(userInput != "exit"):
        if(userInput == "encrypt"):
            in_file = open(input("Input file name: "), 'r')
            out_file = open(input("Output file name: "), 'w')
            password = input("Password: ")
            for line in in_file:
                read_line = in_file.readline()
                encrypted_line = encrypt(read_line, password)
                out_file.write(encrypted_line)
                print(encrypted_line)
            in_file.close()
            out_file.close()

        elif(userInput == "decrypt"):
            in_file = open(input("Input file name: "), 'r')
            out_file = open(input("Output file name: "), 'w')
            password = input("Password: ")
            for line in in_file:
                read_line = in_file.readline()
                decrypted_line = decrypt(read_line, password)
                out_file.write(decrypted_line)
                print(decrypted_line)
            in_file.close()
            out_file.close()

        else:
            print("Invalid choice!")
        userInput = input("Operation (encrypt, decrypt, exit): ")

main()

我可以想到要遵循的两条轨道:

  • 使用返回字符串的
    raw\u input
    ,而不是返回函数的
    input
    (使测试无效
    userInput==“decrypt”
    及类似)
  • 对于文件中的行:
    足以浏览文件,您不需要添加
    read\u line=in\u file.readline()

问题是什么?请不要在没有给出信息的情况下向我们抛出代码,这段代码正在做什么或应该做什么。你得到任何回溯吗?打印(解密行)正在打印什么东西吗?