【发布时间】:2015-12-13 19:40:41
【问题描述】:
我正在为个人项目编写 vigenere 密码,但遇到了索引错误。它说IndexError: list index out of range。
造成这种情况的行是IndexValue = Alphabet.index(keyList[keyIncrement]) + Alphabet.index(plainTextChar)。
所有代码如下:
playing = True
string = ""
Alphabet = ('z','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')
while playing == True:
string = ""
eord = input('Type "d" to "decrypt" and "e" to "encrypt": ')
if eord == 'e':
texte = input ("Type your word to encrypt: ")
key1 = int(input("Choose a key between 1-26: "))
for letter in texte:
number = (ord(letter)) + (key1)
letter=(chr(number))
string = (str(string)) + (str(letter))
print (string)
keyword = input ("Type 'encrypt' code further or 'decrypt' further: ")
if keyword == 'encrypt':
plainText = input("Please enter the plain text: ")
key = input("Please enter the key: ")
keyList = []
keyLength = 0
while keyLength < len(plainText):
for char in key:
if keyLength < len(plainText):
keyList.append(str(char))
keyLength = keyLength + 1
CipherText = []
IndexValue = 0
keyIncrement = 0
for plainTextChar in plainText:
IndexValue = Alphabet.index(keyList[keyIncrement]) + Alphabet.index(plainTextChar)
while IndexValue > 26:
IndexValue = IndexValue - 26
CipherText.append(Alphabet[IndexValue])
keyIncrement = keyIncrement + 1
print (''.join(CipherText))
finish = input('Would you like to go again Y or N')
if finish == 'n' or finish == 'N':
retry = input ("Would you like to go again? Y or N: ")
if retry == 'N' or retry == 'n':
print ("Please exit the window")
import time
time.sleep(1)
import sys
sys.exit()
elif eord == 'd':
texd = input ("Type your word to decrypt: ")
key2 = int(input("Choose a key between 1-16: "))
for letter in texd:
number = (ord(letter)) - (key2)
letter=(chr(number))
string = (str(string)) + (str(letter))
print (string)
keyword = input ("Type 'encrypt' code further or 'decrypt' further: ")
if keyword == 'decrypt':
plainText = input("Please enter the plain text: ")
key = input("Please enter the key: ")
keyList = []
keyLength = 0
while keyLength < len(plainText):
for char in key:
if keyLength < len(plainText):
keyList.append(str(char))
keyLength = keyLength - 1
completeCipherText = []
cipherCharIndexValue = 0
keyIncrement = 0
for plainTextChar in plainText:
cipherCharIndexValue = Alphabet.index(keyList[keyIncrement]) + Alphabet.index(plainTextChar)
while cipherCharIndexValue > 26:
cipherCharIndexValue = cipherCharIndexValue + 26
completeCipherText.append(Alphabet[cipherCharIndexValue])
keyIncrement = keyIncrement - 1
print (''.join(completeCipherText))
finish = input('Would you like to go again Y or N')
if finish == 'n' or finish == 'N':
retry = input ("Would you like to go again? Y or N: ")
if retry == 'N' or retry == 'n':
print ("Please exit the window")
import time
time.sleep(1)
import sys
sys.exit()
正在发生的部分是这样的:
if keyword == 'encrypt':
plainText = input("Please enter the plain text: ")
key = input("Please enter the key: ")
keyList = []
keyLength = 0
while keyLength < len(plainText):
for char in key:
if keyLength < len(plainText):
keyList.append(str(char))
keyLength = keyLength + 1
CipherText = []
IndexValue = 0
keyIncrement = 0
for plainTextChar in plainText:
IndexValue = Alphabet.index(keyList[keyIncrement]) + Alphabet.index(plainTextChar)
while IndexValue > 26:
IndexValue = IndexValue - 26
CipherText.append(Alphabet[IndexValue])
keyIncrement = keyIncrement + 1
print (''.join(CipherText))
代码有什么问题,因为我尝试使用 pycharm,它突出显示了 [keyIncrement],但我不知道如何解决这个问题。感谢您提前提供任何帮助。
【问题讨论】:
-
仔细阅读错误信息。您没有包含任何有用的信息,例如行号等。您需要的所有信息都在那里。最可能的问题是
keyIncrement大于列表keyList的大小。尝试在行前打印索引和列表大小。 -
我会尝试使用 PyCharm 对其进行调试,并在出现错误的行上放置一个断点。这应该让您了解哪个值超出了范围,并帮助您弄清楚是什么将其设置为超出范围的值。
-
'Traceback(最近一次调用最后):文件“/home/owain/Documents/USB 2/##Python Cipher##.py”,第 33 行,在
IndexValue = Alphabet。 index(keyList[keyIncrement]) + Alphabet.index(plainTextChar) IndexError: list index out of range' 。这是错误信息 -
学校项目?昨天有人带着同样的code来到这里。看起来完全一样。错误也在代码的同一部分。检查那里的答案。
-
对不起,我不知道如何很好地使用pycharm。知道和好的教程?
标签: python encryption caesar-cipher vigenere