python:IndexError:字符串索引超出范围

时间:2018-02-24 14:36:21

标签: python

我在python中编码che guevara代码。 我设法进行了加密,但是我有一个问题。

alphabet = ["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"]
association = ["6", "38", "32", "4", "8", "30", "36", "34", "39", "31", "78", "72", "70", "76", "9", "79", "71", "58", "2", "0", "52", "50", "56", "54", "1", "59"]

[...]

#Association de chaque nombre a un caractere :

intermediaire, intermediaire1, finalfinalfinal, stop, nombre = "", "", "", 0, 0
position1 = 0
position2 = 1
first, second, trio = "", "", ""


while b >= stop :
    back = finalfinal[position2]
    intermediaire = finalfinal[position1]

    if(intermediaire in association):
        intermediaire1 = association.index(finalfinal[position1])
    else:
        first = str(finalfinal[position1])
        print "first", first
        print "position1 =", position1
        second = back
        print "back =", back
        trio = first + second
        intermediaire1 = association.index(trio)
        position1 += 1
        position2 +=1

    nombre = int(intermediaire1)

    finalfinalfinal += alphabet[nombre]

    print "finalfinal", finalfinalfinal

    stop += 1
    position1 += 1
    position2 += 1

print "message decrypte : ", finalfinalfinal

控制台的输出:

$python decrypto.py 
decoupe msg :  2
clef reduite de 2 carateres : 20

msg :  46927
clef : 202020
result operation decryptage :  26725
finalfinal s
finalfinal sa
first 7
position1 = 2
back = 2
finalfinal sal
Traceback (most recent call last):
  File "decrypto.py", line 81, in <module>
    back = finalfinal[position2]
IndexError: string index out of range

我尝试了不同的方法,但错误仍然存​​在。

完整代码:

from math import log10
n = lambda x : int(log10(x))+1

x = 2
print "decoupe msg : ", x
clef = input("clef reduite de 2 carateres : ")

while n(clef) != 2:
    clef = input("clef reduite de 2 caracteres ! : ")
print ""

l = "46927"


alphabet = ["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"]
association = ["6", "38", "32", "4", "8", "30", "36", "34", "39", "31", "78", "72", "70", "76", "9", "79", "71", "58", "2", "0", "52", "50", "56", "54", "1", "59"]

b = len(l)

decoup = ""
final = ""
z = 0
w = x
i = 0
d = 0

#Mettre un espace tout les 2 caracteres :

while b > i:
    decoup = l[z:w]
    z=z+x
    w=z+x
    i+=1
    final = final + decoup + ""

print "msg : ", final

#Adapter la clef a la longueur du msg :

clef = str(clef)
if b%x == 0 :
    d = b/x
    clef = (clef + " ")*d
elif b%x != 0 :
    d = b/x+1
    clef = (clef + "")*d

print "clef : " + clef

#Operation de nettoyage :marche pas vraiment 

final.replace(' ', '')
clef.replace(' ', '')

#Operation de decryptage :

finalfinal, ii, w, inter, inn, out = "", 0 , 0, 0, 0, 0

b = 0
b = len(final)

while ii < b:
        inn = int(final[w])
        out = int(clef[w])
        inter = (inn - out) % 10
        finalfinal = finalfinal + str(inter)
        w += 1
        ii += 1

print "result operation decryptage : ", finalfinal

#Association de chaque nombre a un caractere :

intermediaire, intermediaire1, finalfinalfinal, stop, nombre = "", "", "", 0, 0
position1 = 0
position2 = 1
first, second, trio = "", "", ""


while b >= stop :
    back = finalfinal[position2]
    intermediaire = finalfinal[position1]

    if(intermediaire in association):
        intermediaire1 = association.index(finalfinal[position1])
    else:
        first = str(finalfinal[position1])
        print "first", first
        print "position1 =", position1
        second = back
        print "back =", back
        trio = first + second
        intermediaire1 = association.index(trio)
        position1 += 1
        position2 +=1

    nombre = int(intermediaire1)

    finalfinalfinal += alphabet[nombre]

    print "finalfinal", finalfinalfinal

    stop += 1
    position1 += 1
    position2 += 1

print "message decrypte : ", finalfinalfinal

0 个答案:

没有答案