[英]String index out of range (Python)
我正在编写一个使用Caesar Cipher进行编码,解码和破解的程序。
我有这个功能,可以将字符串中的字母移动指定的数量:
def shift(data, shifter):
alphabet = "abcdefghijklmnopqrstuvwxyz"
data = list(data)
counter = 0 # we will use this to modify the list while we iterate over it
for letter in data:
letter = letter.lower()
if letter not in alphabet:
counter += 1
continue
lPos = alphabet.find(letter)
if shifter >= 0:
shiftedPos = lPos + (0 - shifter)
else:
shiftedPos = lPos + abs(shifter)
if shiftedPos >= len(alphabet) - 1: shiftedPos -= len(alphabet)
data[counter] = alphabet[shiftedPos] # update the letter
counter += 1 # advance
data = ''.join(data) # make it into a string again
return data
我有这个功能来破解一个加密的字符串:
def crack(decryptor=None, tries=None):
if decryptor is None and tries is None:
task = getValidInput("Get data from a [f]ile or [s]tdin? >", "Please give either 'f' or 's'.", 'f', 's')
if task == "f": # it's a file
dataFile = getValidFile() # get an open file object
data = dataFile.read() # get the data from the text file. hopefully it's ascii text!
elif task == "s": # we need to get data from stdin
data = input("Enter data to crack >")
tries = getValidInt("Enter tries per sweep >")
else:
data = decryptor
retry = True
shifter = 0
while retry:
for i in range(0, tries):
oput = "Try " + str(i) + ": "
posData = shift(data, shifter)
negData = shift(data, 0 - shifter)
# semitry 1 - positive
oput += posData + ", "
# semitry 2 - negative
negData = ''.join(negData) # make it into a string again
oput += negData
print(oput)
shifter += 1
doRetry = getValidInput("Keep trying (y/n)? > ", "Invalid!", 'y', 'n')
if doRetry == 'n': retry = False
但是,在选择“ y”继续几次之后,我得到以下IndexError
:
Traceback (most recent call last):
File "CeaserCypher.py", line 152, in <module>
crack()
File "CeaserCypher.py", line 131, in crack
negData = shift(data, 0 - shifter)
File "CeaserCypher.py", line 60, in shift
print(alphabet[shiftedPos])
IndexError: string index out of range
为什么会出现此错误,我该如何解决?
IndexError表示您尝试访问的索引不存在。 在字符串中,这意味着您试图在给定点从字符串中获取字符。 如果给定的点不存在,那么您将尝试获取不在字符串内的字符。
“ 0123456” [7]尝试获取字符串中的第7个字符,但是该索引不存在,因此引发了“ IndexError”。
字符串上的所有有效索引均小于字符串的长度(执行len(string)时)。 在您的情况下,Alphabet [shiftedPos]引发IndexError,因为shiftedPos大于或等于字符串“ alphabet”的长度。
据我了解,您想要做的就是像这样越界越过字符串。 “ z”(字符25)增加2,成为字符27。在这种情况下,您希望现在变成字符2(字母“ b”)。 因此,您应该使用模。 用“ alphabet [shiftedPos%len(alphabet)]”替换“ alphabet [shiftedPos]”,我相信这将解决此问题。
顺便说一下,Modulo将数字除以n,然后得到余数。 有效地,它将减去n直到数字小于n(因此它将始终在您希望其处于的范围内)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.