有没有办法让程序接受大写和非大写的输入?

问题描述

正如标题所说,我的程序可以大写运行,但我希望它与非大写和大写输入兼容。它应该作为大写加密返回。

decrypted = b"ABCDEFGHIJKLMnopQRSTUVWXYZ.1234567890 "
encrypted = b"SE2RL.1W5A0Z8D7H4B9M6JX3FTNVQGPUOYCKI "

encrypt_table = bytes.maketrans(decrypted,encrypted)
decrypt_table = bytes.maketrans(encrypted,decrypted)

result = ''
choice = ''
message = ''

while choice != 'X':
    choice = input("\n Do you want to encrypt or decrypt the message?\n E to encrypt,D to decrypt or X to exit program. ")

    if choice == 'E':
        message = input('\nEnter message for encryption: ')
        result = message.translate(encrypt_table)
        print(result + '\n\n')

    elif choice == 'D':
        message = input('\nEnter message to decrypt: ')
        result = message.translate(decrypt_table)
        print(result + '\n\n')

    elif choice != 'X':
        print('You have entered an invalid input,please try again. \n\n')

解决方法

您可以将所有小写字母添加到 decrypted 字符串中,也可以在用户输入中使用 .upper()

此外,这在技术上是一种替代密码。要使其成为真正的“加密”,您需要有一个“解锁”加密文本的密钥。请参阅 Caesar cipher 以获得一个非常简单的示例(实际上它本身就是一种替代密码:P),其中密钥是移位次数。或者参见 AES,它可以说是迄今为止最好的加密算法。