简体   繁体   English

随机密码生成器只生成有限数量的字符

[英]Random password generator only generating limited amount of characters

I am learning Python and today I decided to try and have a go at creating a random password generator.我正在学习 Python,今天我决定尝试使用 go 创建随机密码生成器。 The code generates a random amount of letters, then numbers, then symbols, (based on the amount the user specifies).该代码生成随机数量的字母,然后是数字,然后是符号(基于用户指定的数量)。 These values are all stored in a list where they are each concatenated with one another.这些值都存储在一个列表中,其中每个值都相互连接。 From there the values are all re-randomized to produce a final password.从那里所有的值都被重新随机化以产生最终密码。

If I try to use large numbers as input (above around 40) it errors.如果我尝试使用大数字作为输入(大约 40 以上),则会出错。 It also doesn't always produce the specified length.它也不总是产生指定的长度。 If I input 30 letters, 30 numbers, and 30 symbols, for example, the final password is short of 90 by about 10 if I remember correctly.比如我输入30个字母,30个数字,30个符号,如果我没记错的话,最后的密码就是少了90乘以10左右。 Any help would be appreciated--I think the fix is simple just having trouble finding it.任何帮助将不胜感激——我认为修复很简单,只是找不到它。

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}")

There's two issues with your code:您的代码有两个问题:

  1. The if statements aren't necessary, because the number of iterations is limited by the for loop already. if语句不是必需的,因为迭代次数已经受到for循环的限制。
  2. random.randint() has inclusive arguments on both sides. random.randint()两边都包含 arguments。 So, in this code snippet:所以,在这个代码片段中:
Password = ""
tot = len(generatorList)
for word in generatorList:
      Password += generatorList[random.randint(0,tot)]

.randint() can return tot , which is out of range for the generatorList . .randint()可以返回tot ,这超出了generatorList的范围。

Here is a code snippet that resolves both of these issues:这是解决这两个问题的代码片段:

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}")

Those fix your immediate issues, but there's also a few extra improvements we could make:这些可以解决您眼前的问题,但我们还可以进行一些额外的改进:

  1. Rather than list out the lowercase characters and digits, we can use constants from the string module.我们可以使用string模块中的常量,而不是列出小写字符和数字。
  2. Repeated string concatenation is inefficient .重复的字符串连接效率低下 Use .join() instead.请改用.join()
  3. Use random.choices() to pick the characters to add to our password, then shuffle them all at the end.使用random.choices()来选择要添加到我们密码中的字符,然后在最后将它们全部洗牌。

With all this in mind, we get the following:考虑到所有这些,我们得到以下信息:

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}")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM