凯撒密码上的强力方法不适用于我

问题描述 投票:0回答:1

此代码的输出是:

原始加密文本,然后是最多26个数字的列表

我不知道为什么会这样,您能帮我吗?

P.S .:解密后的文字是“到达黎明”]

很抱歉,如果这是一个不好的问题,这是我第一次这样做。

MAX_KEY_SIZE = 26

file1 = open(r"encryped.txt", "r")

before = file1.read

key = 0
def gettranslatedmessage(key, before):

    translated = ''

    for symbol in before():

        if symbol.isalpha():
          num = ord(symbol)
          num += key

          if symbol.isupper():
                    if num > ord('Z'):
                        num -= 26

                    elif num < ord('A'):

                        num += 26
          elif symbol.islower():
            if num > ord('z'):
             num -= 26
            elif num < ord('a'):
             num+= 26

          translated += chr(num)
        else:
          translated += symbol
    return translated 


for key in range(1, MAX_KEY_SIZE + 1):
   print(key, gettranslatedmessage(key, before))

file1.close()
python caesar-cipher
1个回答
0
投票

在循环的第一次迭代中,您读取了整个文件。您用key == 1翻译并打印该翻译。

[在所有其余的迭代中,文件中没有剩余要读取的内容,因此before()返回空字符串,而for symbol in before():不执行任何操作。对该gettranslatedmessage()的调用返回一个空的translated字符串,并且print(key, gettranslatedmessage(key, before))打印该密钥,然后打印该空字符串。

您应该将文件读入字符串一次,然后将该字符串传递给函数,而不要传递函数。将分配更改为:

before = file1.read()

并将循环更改为:

for symbol in before:
© www.soinside.com 2019 - 2024. All rights reserved.