I am learning Python and today I decided to try and have a go at creating a random password generator. 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. 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. 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}")