首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Caesar Cipher,Python 3,新程序员

Caesar Cipher,Python 3,新程序员
EN

Stack Overflow用户
提问于 2020-04-16 19:18:35
回答 1查看 415关注 0票数 0

第一次发布到Stackoverflow!

我是一个新的程序员,所以请容忍我,因为我还不是最新的所有可能的函数,所以它可能是我目前只是缺少一个函数或方法的知识,可以快速解决我的问题。

我正在尝试编写一个python 3脚本,它可以解密使用caesar密码的加密文本。我目前已经完成了,所以我的代码将加密文本与字母表中的字符串进行比较,然后存储前3个最常见的字母。由于某些原因,标点符号在这里不是问题。

当涉及到解密文本时,程序无法使其超过任何标点符号。它通过转换字母表来表示"E“,因此最常见的字母变成了”E“。我无法理解让python忽略并打印标点符号的方法。我已经制作了一个单独包含标点符号的字符串,但我不知道从这里还能做什么。下面附加了Python 3的代码

感谢大家的帮助!

代码语言:javascript
复制
#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 = "")
EN

回答 1

Stack Overflow用户

发布于 2020-04-16 19:23:33

您应该检查每个位置的代码值是否在您的字母表中。如果不是这样,只需打印代码中的值,然后继续下一次循环。

代码语言:javascript
复制
for i in range(0, len(code)):
    if code[i] not in alphabet:
        print(code[i])
        continue
    a = shift.index("E") + 26 - shift.index(ac)
    print(shift[a + shift.index( code[i] ) ], end = "")
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61248936

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档