1

我正在学习 Python,今天我决定尝试创建一个随机密码生成器。该代码生成随机数量的字母,然后是数字,然后是符号(基于用户指定的数量)。这些值都存储在一个列表中,它们每个都相互连接。从那里,这些值都被重新随机化以产生最终密码。

如果我尝试使用大数字作为输入(大约 40 以上),则会出错。它也不总是产生指定的长度。例如,如果我输入了 30 个字母、30 个数字和 30 个符号,如果我没记错的话,最终的密码大约是 90 乘以 10。任何帮助将不胜感激——我认为修复很简单,只是很难找到它。

Here is my code: 


import random
import itertools

# lists
chars = ['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']
nums = ['0','1','2','3','4','5','6','7','8','9']
syms = ['!','@','#','$','%','^','&','*','+','_']

# inputs
print("Python Password Generator : \n")
charAmount = int(input("How many letters would you like to have in your password? : "))
numAmount = int(input("How many numbers? : "))
symAmount = int(input("How many symbols? : "))

# random num/sym/char

randomNum = ""
for num in nums :
  if int(num) < numAmount :
    randomNum += nums[random.randint(0,9)]


randomSym = ""
for sym in range(0, symAmount):
  if sym < symAmount :
    randomSym += syms[random.randint(0,9)]


randomChar = ""
for char in range(0, charAmount) :
  if sym < charAmount :
    randomChar += chars[random.randint(0, 25)]


# Final Password generator
generatorList = list(itertools.chain(randomNum,randomSym,randomChar))

Password = ""
tot = len(generatorList)
for word in generatorList:
      Password += generatorList[random.randint(0,tot)]

print(f"Your new password is : {Password}")
4

1 回答 1

0

您的代码有两个问题:

  1. 这些if语句不是必需的,因为迭代次数已经受到for循环的限制。
  2. random.randint()双方都有包容性的论据。因此,在此代码段中:
Password = ""
tot = len(generatorList)
for word in generatorList:
      Password += generatorList[random.randint(0,tot)]

.randint()可以返回tot,这超出了 的范围generatorList

这是解决这两个问题的代码片段:

import random
import itertools
# lists
chars = ['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']
nums = ['0','1','2','3','4','5','6','7','8','9']
syms = ['!','@','#','$','%','^','&','*','+','_']

# inputs
print("Python Password Generator : \n")
charAmount = int(input("How many letters would you like to have in your password? : "))
numAmount = int(input("How many numbers? : "))
symAmount = int(input("How many symbols? : "))

# random num/sym/char

randomNum = ""
for num in range(numAmount) :
    randomNum += nums[random.randint(0,9)]

randomSym = ""
for sym in range(0, symAmount):
    randomSym += syms[random.randint(0,9)]

randomChar = ""
for char in range(0, charAmount) :
    randomChar += chars[random.randint(0, 25)]

# Final Password generator
generatorList = list(itertools.chain(randomNum,randomSym,randomChar))

Password = ""
tot = len(generatorList)
for word in generatorList:
     Password += generatorList[random.randint(0,tot - 1)]

print(f"Your new password is : {Password}")

这些解决了您的直接问题,但我们还可以进行一些额外的改进:

  1. string我们可以使用模块中的常量,而不是列出小写字符和数字。
  2. 重复的字符串连接是低效的。改为使用.join()
  3. 用于random.choices()选择要添加到我们密码中的字符,然后在最后将它们全部洗牌。

考虑到所有这些,我们得到以下信息:

import random
import string

chars = string.ascii_lowercase
nums = string.digits
syms = ['!','@','#','$','%','^','&','*','+','_']

print("Python Password Generator : \n")
char_amount = int(input("How many letters would you like to have in your password? : "))
num_amount = int(input("How many numbers? : "))
sym_amount = int(input("How many symbols? : "))

password_characters = random.choices(chars, k=char_amount) + random.choices(nums, k=num_amount) + random.choices(syms, k=sym_amount)
random.shuffle(password_characters)

password = ''.join(password_characters)
print(f"Your new password is : {password}")
于 2022-02-24T03:33:19.347 回答