2017-02-13 11 views
0

"Hey dude、どうやってやっていますか?"と入力すると、 "List index of range"エラーを出力する手作りのテキスト暗号化スクリプトがあります。暗号化するテキストとして「KFC」を暗号化キーとして使用します。Python 3ファイルの "リストのインデックスが範囲外です"エラー

私はより正確にこれを取得する:

Traceback (most recent call last): File "/Users/BCPianist/Google Drive/Project-Cryptonite/NCrypt.py", line 75, in <module> main(userIn, a) File "/Users/BCPianist/Google Drive/Project-Cryptonite/NCrypt.py", line 47, in main currentscrtrank += 1 + scrtlst[scrtrankcounter] IndexError: list index out of range

私はこのスクリプト(メインスクリプトと関数ライブラリ)で使用される2つのファイルを持っています。このエラーがどこから来ている私はちょうどここに... を理解することはできません、私のメインのスクリプトです。おかげですでに

import os 

DEBUG = True 


def printdebug(log: object) -> object: 
    if DEBUG: 
     print(log) 


def loading(sec=10): 
    start = 1 
    input("Press Enter to continue") 
    printdebug("loading...") 
    while start <= sec: 
     printdebug("...") 
     start += 1 


def codepause(): 
    os.system("Pause") 
    input("press enter to continue") 


def listtoint(msglst): 
    # Conversion from list to int 
    """ 
    >>> listtoint([1,2,3,4,5,6,7,8,9,0]) 
    1234567890 
    """ 
    ncryptedkey = 0 
    base = 10 
    for d in msglst: 
     ncryptedkey = base * ncryptedkey + d 
    # printDebugJob: 
    printdebug("The encrypted message is:" + str(ncryptedkey)) 
    return ncryptedkey 


def convdecimal(userin): 
    rank = len(userin) 
    total = 0 
    for character in userin: 
     rank -= 1 # decreasing the letter rank by one 

     letter = ord(character) 
     if letter < 32: 
      pass 
     elif letter == 27: 
      pass 
     else: 
      rankmult = 255 ** rank # Making a multiplier 
      lettervalue = letter * rankmult # Multiplying the letter by this multiplier 
      total += lettervalue # Adding the letter's value to the total 


def letterprint(nb): 
    nb = chr(nb) 

    print(nb, end='') 

import random 
from cryptolib import * 

# !/usr/bin/env python 
# -*- coding: UTF-8 -*- 
# enable debugging 
print("Content-Type: text/plain;charset=utf-8") # Definit le code d'ecriture 
print() # Insere une ligne d'espacement. 


def main(userin, numberedsecretkey): # Fonction principale (le gros de l'encryption) 
    """ 
    This is my main. 
    >>> main("Allo", "Hey") 
    10842839726 
    """ 
    comments = True # Definit si les commentaires lors de l'impression sont actif ou non (False les desactive) 
    total = convdecimal(userin) # Time to deal with the encryption key: 

    scrtkeytotal = convdecimal(numberedsecretkey) 

    # converting numbered message to list 
    msglst = [int(elem) for elem in str(total)] 
    if comments == True: 
     printdebug("The initial numbered message is:%s " % msglst) 
    # converting numbered key to list 
    scrtlst = [int(thingy) for thingy in str(scrtkeytotal)] 
    if not comments != True: 
     printdebug("The initial encryption key is:%s" % scrtlst) 

    # Attempting Encryption 

    scrtrankcounter = 0 
    currentscrtrank = scrtlst[scrtrankcounter] 

    while currentscrtrank < len(msglst): 
     randomgen = random.randint(0, 9) 
     msglst.insert(currentscrtrank, randomgen) 
     if comments: 
      printdebug(str(randomgen) + " was inserted at rank: " + str(
       scrtrankcounter) + ", therefore, at index position: " + str(currentscrtrank) + ".") 
      printdebug("List now appears as: " + str(msglst)) 
     scrtrankcounter += 1 
     if scrtrankcounter > len(scrtlst): 
      scrtrankcounter = 0 

     currentscrtrank += 1 + scrtlst[scrtrankcounter] 

    return listtoint(msglst) 


def convdecimal(userin): 
    rank = len(userin) 
    total = 0 
    for character in userin: 
     rank -= 1 # decreasing the letter rank by one 

     letter = ord(character) 
     if letter < 32: 
      pass 
     elif letter == 27: 
      pass 
     else: 
      rankmult = 255 ** rank # Making a multiplier 
      lettervalue = letter * rankmult # Multiplying the letter by this multiplier 
      total += lettervalue # Adding the letter's value to the total 

    return total 


if __name__ == "__main__": 
    userIn = input("Enter the word/Sentence you want to encrypt:") 
    a = input("Enter a password with which you would like to encrypt your message:") 
    print(userIn) 
    main(userIn, a) 

そして、ここでは私の関数ライブラリファイルです!

答えて

1

私はこれをテストしていませんが、より大きいか等しいかどうかを確認する必要があります。おそらく1つのインデックスが高すぎます。例えば

if scrtrankcounter >= len(scrtlst): 
     scrtrankcounter = 0 

    currentscrtrank += 1 + scrtlst[scrtrankcounter] 

scrtlstは5の長さを持っている場合、最高インデックスが4であるので、あなたはscrtlst[5]をしようとした場合はエラーを与えるでしょう。

関連する問題