在测距时不会抛出错误
一串
. 一种可以通过添加
ck = string[i+len(sequence)]
try
封锁。
试试这个。。。
def getIndex(string, sequence):
for i in range(len(string)):
if string[i] == sequence[0]:
try:
# this will result IndexError if index out of range
ck = string[i+len(sequence)]
if string[i+1:(i+1+len(sequence)-1)] == sequence[1:]:
return i
else:
continue
except IndexError:
print('Array out of bounds substring doesnt exist')
else:
return 'not found'
print(getIndex('skyscrapper', 'erss'))