【问题标题】:I am trying to create Caesar Ciphers functions in Python, but they seem to work only with lower case letter, how to work with upper case?我正在尝试在 Python 中创建凯撒密码函数,但它们似乎只适用于小写字母,如何使用大写字母?
【发布时间】:2018-01-15 06:11:43
【问题描述】:

我正在尝试在 Python 中创建凯撒密码函数,但它们似乎只适用于小写字母,如何使用大写字母?

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)

【问题讨论】:

  • 您希望大写字母发生什么?是不是应该是两个域,大写换大写,小写换小写?还是应该将大写字母转换为小写字母?您的问题没有得到充分说明。

标签: python math caesar-cipher letter


【解决方案1】:

我相信您需要将大写字母添加到字典中。否则,程序不知道将它们映射到哪个数字。 我建议设置变量 letters = "abcd..." 然后添加 letters = letters + letters.upper()

在程序的开头。

另外,将所有 26 更改为 52。 最后,将“abcd...”替换为letters

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-02
    相关资源
    最近更新 更多