How can I make my Vigenère Cipher ignore spaces in the original message

前端 未结 1 1374
甜味超标
甜味超标 2021-01-28 07:54

Im trying to make a Vigenère Cipher but I can\'t seem to find a way to implement a feature that ignores in-putted white spaces when entering the message and then printing the fi

1条回答
  •  无人及你
    2021-01-28 08:59

    You just need to add an else statement in translateMessage() to add the space to the output like this

    def translateMessage(key, message, mode):
        translated = ""
    
        keyIndex = 0
        key = key.upper()
    
        for symbol in message:
            xyz = alphabet.find(symbol.upper())
            if xyz != -1:
                if mode == 'encrypt' or 'e':
                    xyz += alphabet.find(key[keyIndex]) + 1
                elif mode == 'decrypt' or 'd':
                    xyz -= alphabet.find(key[keyIndex]) + 1
    
                xyz %= len(alphabet)
    
                if symbol.isupper():
                    translated += alphabet[xyz]
                elif symbol.islower():
                    translated += alphabet[xyz].lower()
    
                keyIndex += 1
                if keyIndex == len(key):
                    keyIndex = 0
            else : translated += symbol #this will add space as it is
    
        return translated
    

    0 讨论(0)
提交回复
热议问题