2017-11-06 16 views
0

このエラーはなぜ発生しますか?このエラーを解決するには、13文字シフトするための暗号を使用します。

トレースバック(最新の呼び出しの最後): はIndexErrorで ファイル "パイソン"、6行目、:範囲外の文字列インデックス

このコードに

Cipher = input("Enter encrypted text:") 
length = len(Cipher) 
start = 0 
decrypt="" 
while length!=Cipher: 
    asc=ord(Cipher[int(start)]) #String index out of range 
    if asc <=95: 
    decrypt=decrypt+Cipher[start] 
    elif asc >95 and asc <=110: 
    num = asc 
    num=num+13 
    decrypt=decrypt+chr(num) 
    elif asc>110 and asc<=122: 
    num = asc 
    num=num-13 
    decrypt=decrypt+chr(num) 
    else: 
    decrypt=decrypt+"" 
    start=start+asc 
print("DONE!") 

どのように私はそれを解決するのですか?

+0

を使用すると、5行目にあり何をしていますか?文字列の長さと文字列自体の比較それは意味をなさない – Basti

答えて

0

暗号は文字列であるため、長さには決して等しくない整数です。長さは、すでにintですので、 int型(長さ)は、必要ではないでしょう...

Cipher = input("Enter encrypted text:") 
length = len(Cipher) 
start = 0 
decrypt="" 
while start!=length: #check for termination condition 
    asc=ord(Cipher[start]) 
    if asc <=95: 
     decrypt=decrypt+Cipher[start] 
    elif asc >95 and asc <=110: 
     num = asc 
     num = num+13 
     decrypt=decrypt+chr(num) 
    elif asc>110 and asc<=122: 
     num = asc 
     num = num-13 
     decrypt=decrypt+chr(num) 
    else: 
     decrypt=decrypt+"" 
    start=start+1 # increment counter 
0
Cipher = list(input("Enter encrypted text:")) 
length = len(Cipher) 
start = 0 
decrypt="" 
while start != length: 
    asc=ord(Cipher[start]) 
    if asc <=95: 
    decrypt=decrypt+Cipher[start] 
    elif asc >95 and asc <=110: 
    num = asc 
    num=num+13 
    decrypt=decrypt+chr(num) 
    elif asc>110 and asc<=122: 
    num = asc 
    num=num-13 
    decrypt=decrypt+chr(num) 
    start=start+1 

print("DONE! Result: " + decrypt) 

あなたはここでそれを試すことができます:https://repl.it/Nn05/0

関連する問題