any()不返回true?

时间:2017-10-22 03:40:08

标签: python

我正在研究ceaser密码的变体。当我输入加密消息时,我的decrypt()函数应该使用不同的旋转值进行解密,直到一个常用字为止,例如。弹出“the”或“time”或“attack”。出于测试目的,我正在使用诸如“继续攻击基础”之类的消息,因此当any(word.upper() in plainText for word in subStrings)部分运行时,它应该返回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

2 个答案:

答案 0 :(得分:0)

我认为问题出在这一行

cipherIndex = alphanumericAlphabet.index(character)

您必须检查character.upper()的索引,因为您的字母数字字母只包含大写字母。

答案 1 :(得分:0)

为字母的字母数字字母列表添加空格以便处理或删除空格:

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)