【发布时间】:2020-07-29 14:46:21
【问题描述】:
第一次在 Stackoverflow 上发帖!
我是一个新程序员,所以请多多包涵,因为我还没有掌握所有可能的功能,所以我可能只是目前缺少可以解决我问题的函数或方法的知识问题很快。
我正在尝试制作一个可以解密使用凯撒密码的加密文本的 python 3 脚本。 我目前已经做到了,所以我的代码将加密文本与字母字符串进行比较,然后存储前 3 个最常见的字母。出于某种原因,标点符号在这里不是问题。
在解密文本时,程序无法使其超过任何标点符号。 它通过移动字母表来解密,以便最常见的字母变成“E”。我无法理解让 python 简单地忽略并打印标点符号的方法。我已经分别制作了一个包含标点符号的字符串,但我不知道从这里还能做什么。 代码附在下面,在 Python 3 中
感谢所有帮助!
#string used for comparing code to
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#strings for shifting the code
shift = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ"
punctuation = ".,!;:?"
#ask user for the encrypted text
code = input("Enter text: ")
#variables storing the most common letters and the # of times they come up
common1 = 0
common2 = 0
common3 = 0
ac = 0
bc = 0
cc = 0
for i in range(0, len(alphabet)):
# print number of times each letter comes up --- print( str(alphabet[i]) + " is present " + str(code.count(alphabet[i])) + " number of times" )
#finding the number of times the most common letter comes up
if code.count(alphabet[i]) > common1:
common3 = common2
cc = bc
common2 = common1
bc = ac
common1 = code.count(alphabet[i])
ac = alphabet[i]
elif code.count(alphabet[i]) > common2:
common3 = common2
cc = bc
common2 = code.count(alphabet[i])
bc = alphabet[i]
elif code.count(alphabet[i]) > common3:
common3 = code.count(alphabet[i])
cc = alphabet[i]
print("Most common letter, " + str(ac) + ", comes up " + str(common1) + " times")
print("Second most common letter, " + str(bc) + ", comes up " + str(common2) + " times")
print("Third most common letter, " + str(cc) + ", comes up " + str(common3) + " times")
for i in range(0, len(code)):
a = shift.index("E") + 26 - shift.index(ac)
print(shift[a + shift.index( code[i] ) ], end = "")
【问题讨论】:
标签: python encryption caesar-cipher