如何让我的Caesar Cipher工作?

时间:2013-11-15 02:09:25

标签: cryptography

这是我目前为我的Caeser Cipher计划所做的。

 import string
    character = []
    message = raw_input('What is your message? ').lower()
    shift = raw_input('What is your shift key? ')
    code = raw_input('Would you like to cipher(c) or decipher(d)? ')
    if str(code) == 'd':
        for character in message:
            number = ord(character) - int(shift)
            if number <= 96:
                number = ord(character) + 26 - int(shift)
            character = chr(number)
    elif str(code) == 'c':
        for character in message:
            number = ord(character) + int (shift)
            if number >= 123:
                number = ord(character) - 26 + int(shift)
            character = chr(number)
    print(str(character))

每次我使用这个程序时,我都会收到加密或解密的消息,该消息只包含我为消息输入的行的最后一个字母。我不确定如何打印出我的整个加密或解密信息。

1 个答案:

答案 0 :(得分:2)

问题是你只能在for循环之外打印一次。

您可以在for循环中移动print语句。

if str(code) == 'd':
    for character in message:
        number = ord(character) - int(shift)
        if number <= 96: 
            number = ord(character) + 26 - int(shift)
        character = chr(number)
        print( str(character))
elif str(code) == 'c':
    for character in message:
        number = ord(character) + int (shift)
        if number >= 123:
            number = ord(character) - 26 + int(shift)
        character = chr(number)
        print(str(character))