2017-09-29 44 views
0

誰でも私のプレーンテキストメッセージの1文字だけを暗号化する理由を教えていただけますか?メッセージは「真夜中に帆船」で、暗号化キーは4です。私はtをxに移動することしかできず、残りのメッセージは印刷されません。私は何が欠けていますか?シーザー暗号暗号化Python

#request the message from the user 
def InputMessage(): 
    PlainText = input("Enter the message you would like to encrypt: ") 
    return PlainText 

#encrypt the message 
def CaesarShift(PlainText): 

    #initialize variables 
    alpha = ['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'] 
    Key = int(input("Enter the encryption key: ")) 
    CipherText = "" 

    #skip over spaces in the message 
    for ch in PlainText: 
     if ch == " ": 
      pass 
     else: 

      #encrypt the message 
      index = alpha.index(ch) 
      NewIndex = index + Key 
      ShiftedCharacter = alpha[NewIndex] 
      CipherText += ShiftedCharacter 
     return CipherText 


#main program start 
def main(): 
    PlainText = InputMessage() 
    CipherText = CaesarShift(PlainText) 

    #print the encrypted message 
    print("Encrypted message: " + CipherText) 

#main program end 
main() 
+0

あなたのリターンステートメントをインデントしてください!ループの内側から、最初の反復の後に戻ります。 – schwobaseggl

答えて

0

return文はループ内にあるため、関数は最初の文字を暗号化した後に戻ります。

returnステートメントのインデントレベルがforループのインデントレベルと同じであることを確認する必要があります。

+0

@MikeSilvaまた、新しいインデックスにモジュロを適用して境界内にとどまる必要があります; – schwobaseggl

+0

また、1行に集約することもできます。 – Henry

関連する問題