python 3文件中的“列表索引超出范围”错误

本杰曼

输入“嘿哥们,你好吗?”时,我有一个自制的文本加密脚本,该脚本输出“列表索引超出范围”错误。作为要加密的文本,将“ KFC”作为加密密钥。

我得到的更准确:

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

我在此脚本中使用了两个文件(主脚本和函数库)。我只是不知道此错误来自何处...这是我的主要脚本:

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)

这是我的函数库文件:

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='')

谢谢已经!

彼德

我尚未对此进行测试,但是您似乎需要将大于号换为大于或等于,因为它可能会使1索引过高。

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

    currentscrtrank += 1 + scrtlst[scrtrankcounter]

例如,如果scrtlst长度为5,则最高索引为4,因此,如果尝试使用,则会出现错误scrtlst[5]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

解决错误:Python中的“列表索引超出范围”

python中的列表索引超出范围错误

python:代码中的列表索引超出范围错误

python中的“IndexError:列表索引超出范围”错误

Python错误:列表索引超出范围

IndexError:Python 3中的列表索引超出范围

列表索引超出范围-在python中

python中的列表索引超出范围错误,但索引在范围内怎么办?

Python字典索引错误:列表索引超出范围

python随机索引错误:列表索引超出范围

Python,列表错误:IndexError:列表索引超出范围

Python 3“索引错误:弹出索引超出范围”

如何解决列表索引超出范围的错误Python?

Python 抛出列表索引超出范围错误

IndexError:列表索引超出范围错误python

嵌套JSON的Python测试-列表索引超出范围错误

python迷宫求解器列表索引超出范围错误

Python DJANGO错误IndexError在/列表索引超出范围

Python Beautiful Soup错误:列表索引超出范围

macOS python 2.7 错误:IndexError:列表索引超出范围

Gurobi Python 错误:列表索引超出范围

python列表中的IndexError-“列表索引超出范围”

Python读取XML文件不断出现错误“列表索引超出范围”

python中的缩进错误和索引超出范围错误

python 3.x上的列表索引超出范围错误

Python 3:IndexError:列表索引超出范围

Python3:列表索引超出范围

python 3 IndexError:列表索引超出范围

IndexError:列表索引超出范围(Python 3)