我正在学习 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}")