any()不返回true?

2024-10-03 13:18:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在研究凯撒密码的一种变体。当我输入加密的消息时,我的decrypt()函数应该用不同的旋转值继续解密,直到出现一个公共词,例如“the”或“time”或“attack”。出于测试的目的,我使用了诸如“procedutoattackthebase”这样的消息,因此当any(word.upper() in plainText for word in subStrings)部分运行时,它应该返回true,但是它没有返回true。我目前的代码如下:

def decrypt(encryptedMessage):
    alphanumericAlphabet = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]   # List of every letter in the alphabet

    message = (str(encryptedMessage).strip("\n")).upper()

    subStrings = ["The", "Base", "Proceed", "North", "West", "South", "East", "Hours", "Dawn", "Attack", "Defend", "Shoot", "Bearing", "Enemy", "Position", "Move", "That", "Fast", "Time", "Rise", "Loss", "Win", "Victory"]
    plainText = ""
    messageFound = False
    rotationValue = 0

    while messageFound != True:
        if any(word.upper() in plainText for word in subStrings): 
            messageFound = True
        else:
            plainText = ""
            for character in message:
                cipherIndex = alphanumericAlphabet.index(character.upper())
                plainIndex = cipherIndex - rotationValue
                if plainIndex < 0:
                    plainIndex += 36
                plainText += alphanumericAlphabet[plainIndex]
        rotationValue += 1

    print "Decrypted Message:", plainText, "\n", "Rotation:", rotationValue

    return plainText, rotationValue

Tags: theintrue消息foranyupperword
2条回答

在字母数字数字列表中为要处理的字符添加空格或去掉空格:

def decrypt(encryptedMessage):
    alphanumericAlphabet = [" ","A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]   # List of every letter in the alphabet

    message = (str(encryptedMessage).strip("\n")).upper()

    subStrings = ["The", "Base", "Proceed", "North", "West", "South", "East", "Hours", "Dawn", "Attack", "Defend", "Shoot", "Bearing", "Enemy", "Position", "Move", "That", "Fast", "Time", "Rise", "Loss", "Win", "Victory"]
    plainText = ""
    messageFound = False
    rotationValue = 0

    while messageFound != True:
        if any(word.upper() in plainText for word in subStrings):        
            messageFound = True
        else:
            plainText = ""
            for character in message:
                cipherIndex = alphanumericAlphabet.index(character)
                plainIndex = cipherIndex - rotationValue
                if plainIndex < 0:
                    plainIndex += 36
                plainText += alphanumericAlphabet[plainIndex]
        rotationValue += 1

    print("Decrypted Message:", plainText, "\n", "Rotation:", rotationValue)
    return plainText, rotationValue
decrypt("Proceed to attack the base")
Decrypted Message: PROCEED TO ATTACK THE BASE 
 Rotation: 2
Out[1852]:
('PROCEED TO ATTACK THE BASE', 2)

我想问题出在这方面

cipherIndex = alphanumericAlphabet.index(character)

您必须检查character.upper()的索引,因为您的alphanumericAlphabet只包含大写字符。你知道吗

相关问题 更多 >