【问题标题】:Python : "IndexError: list index out of range" Password generatorPython:“IndexError:列表索引超出范围”密码生成器
【发布时间】:2021-04-05 17:27:12
【问题描述】:

我一直在尝试使用 secrets 模块在 python 中创建密码生成器。 我首先询问用户所需的密码长度,然后在 3 个字符列表(字母、数字、符号)之间随机选择。然后我通过循环将结果一一打印出来。

我的问题是程序可以正常工作,直到用户询问大约 12 个字符。然后我收到以下错误IndexError: list index out of range,在搜索了几个小时后,我无法找到答案。

对不起,如果这个问题看起来很基础,但我还是个初学者,只想改进!

这是我的代码:

import secrets
# importing modules

doRun = True
while doRun:
    length = int(input("How many characters do you want you password to have ? "))
    # Asking the user for the length of the password

    letCar = ["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", "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"]
    # List for characters
    numCar = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"]
    # List for numbers
    symCar = ["!", "@", "?"]
    # List for symbols
    password = []
    # Initializing list for password

    for i in range(0, length):
        selector = secrets.randbelow(3)
        # Randomly choosing from which list to pick
        if selector == 0:
            # Characters list
            chosenCar = secrets.choice(letCar)
            password.append(chosenCar)
            letCar.remove(chosenCar)
        elif selector == 1:
            # Numbers list
            chosenCar = secrets.choice(numCar)
            password.append(chosenCar)
            numCar.remove(chosenCar)
        elif selector == 2:
            # Symbols list
            chosenCar = secrets.choice(symCar)
            password.append(chosenCar)
            symCar.remove(chosenCar)

    print("password is : ", end="")
    for i in range(len(password)):
        print(password[i], end="")
    print()
    doRun = input("Again ? (Yes --> True // No --> False) : ")

错误是:

Traceback (most recent call last):
  File "E:\Bureautique\Outils\Programmation\Python\Projects\PWHash\main.py", line 35, in <module>
    chosenCar = secrets.choice(symCar)
  File "C:\Users\Arthur\AppData\Local\Programs\Python\Python39\lib\random.py", line 347, in choice
    return seq[self._randbelow(len(seq))]
IndexError: list index out of range

感谢您的帮助!

【问题讨论】:

  • 触发错误的所选长度值是多少?
  • 它在长度 >= 10 时开始出错,但不是每次都出错。这让我很感兴趣,它有时会打印结果,有时会返回错误。我也收到较低值的错误,但似乎更罕见。
  • Zakariyya 是对的。此外,如果您尝试生成密码,最好将所有字符放在一个大列表中,而不是将其拆分,并且不要从列表中删除任何字符。最好的密码是不可预测的:通过删除字符,您可以更轻松地猜测下一个字符是什么(它不能是密码中已经存在的任何字符)并且通过将字符放在不同的列表中,您使特殊字符比字母更容易出现
  • 好吧,您正在从列表中随机删除项目,因此如果您从 symCar 中删除所有元素,它可能会在 4 次后崩溃,但如果从较长的列表中进行大量采样,则列表可能会持续更长时间比如 letCar。
  • 是的,这可能会奏效!另外,感谢您的建议,我将对此进行修改以使程序更强大且更难预测。无论如何,该程序不应该每天都使用,而更像是我的一个项目想法,我认为这样做会很有趣。

标签: python python-3.x


【解决方案1】:

我认为这是因为你从汽车列表中删除了物品,这样当汽车被选中足够的时间时,在Symcars的情况下,您的程序崩溃的行,只有3,它是一个空的列表,因此如果再次选择汽车,则列表中没有任何内容可供秘密模块查找

【讨论】:

  • 确实,这可能会解决问题,我没有考虑这个案例!我会尝试更正它,然后会回复你。非常感谢!
【解决方案2】:

我不知道这是否是您正在寻找的,但这就是它对我的工作方式

for i in range(0, length):
        selector = secrets.randbelow(3)
        # Randomly choosing from which list to pick
        if selector == 0:
            # Characters list
            chosenCar = secrets.choice(letCar[i])
            password.append(chosenCar)
            letCar.remove(chosenCar)
        elif selector == 1:
            # Numbers list
            chosenCar = secrets.choice(numCar[i])
            password.append(chosenCar)
            numCar.remove(chosenCar)

【讨论】:

    【解决方案3】:

    问题是每次从 symCar 中选择一个项目时都会删除一个项目。这将在重复三次后清空列表。要解决此问题,您可以删除 .remove 行。虽然这允许在您的密码中包含多个单个字符,但这有助于使其不可预测。

    另一个选项是添加一种方式,让列表在所有项目都被删除后重新填充。一个例子如下所示。

    if symCar == []:
        symCar = ["!", "@", "?"]
    

    【讨论】:

      【解决方案4】:

      问题是我在选择后从列表中删除了一个项目。它有时会生成一个空列表,因此会出现错误。正如 Matthew 所建议的,我通过列出其中所有字符的大列表来纠正此问题,并且我不会从列表中删除所选字符以降低程序的可预测性。

      新编辑的节目是这个:

      import secrets
      import random
      # importing modules
      
      doRun = True
      while doRun:
          length = int(input("How many characters do you want you password to have ? "))
          # Asking the user for the length of the password
      
          car = ["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", "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", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "!",
                 "@", "?"]
          # Characters list
      
          password = []
          # Initializing list for password
      
          for i in range(0, length):
              random.shuffle(car)
              password.append(secrets.choice(car))
              # Picking an item form the car list and appending it to the password list after a shuffle
      
          print("password is : ", end="")
          for i in range(len(password)):
              print(password[i], end="")
          print()
          doRun = input("Again ? (Yes --> True // No --> False) : ")
      

      感谢大家的帮助!

      【讨论】:

        猜你喜欢
        • 2016-08-25
        • 2017-04-05
        • 2012-07-15
        • 2013-06-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多