Python 使用感叹号对Caesar密码函数进行加密

Python 使用感叹号对Caesar密码函数进行加密,python,encryption,caesar-cipher,Python,Encryption,Caesar Cipher,有人能帮我做一个函数来加密包含感叹号的消息吗。你好!你好 现在,我的(HOWDY!Hello.)函数的输出是 当它真的应该是: Your translated text is: MTBID! Mello. 我的完整代码: alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" message = "" encryptedmessage = " " keynumber = 0 def encrypt(): globa

有人能帮我做一个函数来加密包含感叹号的消息吗。你好!你好 现在,我的(HOWDY!Hello.)函数的输出是

当它真的应该是:

Your translated text is:
MTBID! Mello.
我的完整代码:

alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
message = ""
encryptedmessage = " "
keynumber = 0

def encrypt():
 global message
 global encryptedmessage
 global keynumber
 print()
 print()
 message = str(input("Enter your message:"))
 print()
 print()
 keynumber = int(input("Enter the key number (1-26)"))
 print()
 print()
 for i in message:
  position = alphabet.find(i)
  newposition = (position+5)%26
  encryptedmessage +=alphabet[newposition]
 print("Your translated text is:")
 print(encryptedmessage)


def decrypt():
 global message
 global encryptedmessage
 global keynumber
 message = str(input("Enter your message:"))
 keynumber = int(input("Enter the key number (1-26)"))


action = input("Do you wish to encrypt or decrypt a message?")

if action == "encrypt":
 encrypt()
if action == "decrypt":
 decrypt()
字母表。如果
i
不在
字母表中,则查找(i)
将为
-1
。发生这种情况时,您应该将
i
复制到加密邮件中,而不是对其进行编码

position = alphabet.find(i)
if position == -1:
    encryptedmessage += i
else:
    newPosition = (position + 5) % 26
    encryptedmessage += alphabet[newPosition]

我该怎么做呢?这就是我在答案中所展示的:
encryptedmessage+=I
哦,谢谢你,(你好!)的第一部分可以工作,但第二部分(你好。)不工作。输出是:(MTBID!MJQQT。)当输出应该是(MTBID!Mello。)@Banger123字母表有52个字符,但你使用的是
(position+5)%26
,所以它会将所有内容转换为大写字母。是否可以将此代码添加到我的解密函数中,只需稍作更改?
position = alphabet.find(i)
if position == -1:
    encryptedmessage += i
else:
    newPosition = (position + 5) % 26
    encryptedmessage += alphabet[newPosition]