0

到目前为止,这就是我的 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))

每次我使用这个程序时,我都会返回我为消息键入的行的最后一个字母的加密或解密消息。我不确定如何打印出我的整个加密或解密消息。

4

1 回答 1

2

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

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

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))
于 2013-11-15T03:06:31.687 回答