나는 Vigenere 암호를 만들려고합니다. 메시지를 암호화하려고하면 다음 오류가 발생합니다.파이썬 Vigenere 튜플 오류
cipherCharIndexValue = baseAlphabet.index(keyList[keyIncrement]) + baseAlphabet.index(plainTextChar)
ValueError: tuple.index(x): x not in tuple
어떤 오류가 발생했는지 확실하지 않습니다.
당신이 python2.x을 사용하고있는 것으로 보인다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.
난 당신의 코드를 실행 취소 들여 쓰기를 반복하면서 첫까지 모든 것을 포함, I 라인 (28) 상에 IndentationError있어 후 라인 2에 IndentationError를 가지고 시도 . 그 줄을 한 칸 공백으로 들여 쓰기를 한 후에, 나는'NameError : name 'baseAlphabet'not defined'을 얻었습니다. 실제로 문제를 나타내는 [mcve]를 게시하십시오. – Kevin
'keyList [keyIncrement] 또는'plainTextChar'는'baseAlphabet'에 없습니다. 정확히 어떤 값이 실패했는지 알아 내려면 문제 해결을해야합니다. – glibdud
코드를 편집 해 주셔서 감사합니다. 그것은 지금 나를 위해 달린다. 파이썬 2를 사용하여 실행하고 첫 번째 프롬프트에는''foo "'를, 두 번째 프롬프트에는''bar"'를 입력하면 충돌없이 실행되고'gof'가 출력됩니다. 프로그램을 중단시키기 위해 입력 한 입력 내용을 설명하십시오. – Kevin