我试图在Python中创建Caesar Ciphers函数,但它们似乎只适用于小写字母,如何使用大写字母?
a = dict(zip("abcdefghijklmnopqrstuvwxyz",range(26)))
b = dict(zip(range(26),"abcdefghijklmnopqrstuvwxyz"))
key = int(input('Enter the key:'))
plaintext = (input('Enter your message:'))
ciphertext = ""
for c in plaintext:
if c.isalpha():
ciphertext += b[ (a[c] + key)%26 ]
else: ciphertext += c
plaintext2 = ""
for c in ciphertext:
if c.isalpha():
plaintext2 += b[ (a[c] - key)%26 ]
else: plaintext2 += c
print(plaintext,",",ciphertext,",",plaintext2)
我相信你需要将大写字母添加到字典中。否则,程序不知道将它们映射到的号码。我建议设置变量letters = "abcd..."
,然后添加letters = letters + letters.upper()
在计划的开头。
此外,将所有26s更改为52s。最后,用letters
替换“abcd ......”。