2013-11-15 91 views
0

這是我迄今爲止的Caeser密碼程序。如何讓我的Caeser密碼工作?

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)) 

我每次使用這個程序,我回來只是我鍵入郵件行的最後一個字母的加密或解密的消息。我不知道如何打印出我的整個加密或解密消息。

回答

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)) 
+0

非常感謝!我完全按照你解釋它的方式得到它:) – JPtheK9

+0

在旁註中,如何列出句子中的字母,而不是在每個字母之間留有空格? – JPtheK9