私はVigenere暗号を作ろうとしています。メッセージを暗号化しようとすると、次のエラーが発生します。Python Vigenereタプルエラー
cipherCharIndexValue = baseAlphabet.index(keyList[keyIncrement]) + baseAlphabet.index(plainTextChar)
ValueError: tuple.index(x): x not in tuple
どのようなエラーが発生しているのかわかりません。
baseAlphabet = ('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')
plainText = input("Please enter the plain text")
key = input("Please enter the key word")
keyList = []
keyLength = 0
while keyLength < len(plainText):
#Adds the users entered key into a list character by character.
#Also makes the key the same length as plainText
for char in key:
if keyLength < len(plainText):
keyList.append(str(char))
keyLength = keyLength + 1
#The variable each processed letter is appended to
completeCipherText = []
#This is the value used to temporaily store the ciphertext character during the iteration
cipherCharIndexValue = 0
keyIncrement = 0
#iterates through the plain text
for plainTextChar in plainText:
#Adds the base alphabets index value of the key and the plain text char
cipherCharIndexValue = baseAlphabet.index(keyList[keyIncrement]) + baseAlphabet.index(plainTextChar)
while cipherCharIndexValue > 25:
#makes the addition value under 26 as to not go out of range of base alphabet tuple
cipherCharIndexValue = cipherCharIndexValue - 26
#appends the ciphertext character to the completeCipherText variable.
#The character is the index of the key + index of the plainTextChar from baseAlphabet
completeCipherText.append(baseAlphabet[cipherCharIndexValue])
#Moves onto the next key
keyIncrement = keyIncrement + 1
print ('').join(completeCipherText)#Makes the result a strings for printing to the console.
その行を一つのスペースでインデントした後、 'NameError:name 'baseAlphabet' not defined'を取得しました。あなたの問題を実際に示す[mcve]を投稿してください。 – Kevin
'keyList [keyIncrement]または' plainTextChar'は 'baseAlphabet'にありません。どの値が正確に機能していないかを調べるには、トラブルシューティングを行う必要があります。 – glibdud
コードを編集していただきありがとうございます。それは今私のために走る。 Python 2を使って実行し、最初のプロンプトに '' foo "'を、2番目のプロンプトに '' bar "'を入力すると、クラッシュせずに実行され、 'gof'が出力されます。あなたのプログラムをクラッシュさせるために入力した入力内容を記述してください。 – Kevin