私はテキスト入力が与えられ、出力は入力ですが、アルファベットで2に沿ってシフトされるような暗号を作っています。例えば、 "hi"は "jk"に変わります。私は "y"が "b"に変わることができるようにリストをラップするのに問題があります。 プレーンテキストは設定入力です。 キーがシフトする2Pythonでラップリストを囲んで
charset=["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"] # characters to be encrypted
def caesar_encrypt(plaintext,key):
plaintext = plaintext.upper() # convert plaintext to upper case
ciphertext = "" # initialise ciphertext as empty string
for ch in plaintext:
if ch == " ":
pass
else:
index = charset.index(ch)
newIndex = index + key
shiftedCharacter = charset[newIndex]
ciphertext += shiftedCharacter
print(ciphertext)
return ciphertext
試し 'newIndexが=(インデックス+キー)%LEN(文字セット)'(_integer modulo_を満たす) –
インデントは単なる問題を引き起こす可能性が台無しにされています。 – tadman
もう一つの注意として、 'charset =" ABCDEFGHIJKLMNOPQRSTUVWXYZ "'という言葉だけでなく、はるかにコンパクトです。 'import string'を実行してから' string.ascii_uppercase'を使うこともできます。 –