I am creating a password generator the takes the length of the desired password, number of letters, as well as the number of numbers. The password needs to contain uppercase letters as well as numbers and special characters. I am having trouble figuring out how to specify the number of letters and numbers in the password. This is what I have so far:
import random
charslet ="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
charsnum = "1234567890"
charssym = "!@#$%^&*"
def password():
pwd = ""
passlen = int(input("How long do you want the password to be?: "))
passlet = int(input("How many letters do you want in your password?: "))
passnum = int(input("How many numbers do you want in your password?: "))
passsym = int(passlen - (passlet + passnum))
chars = ""
for let in range(passlet):
chars += random.choice(charslet)
for num in range(passnum):
chars += random.choice(charsnum)
for sym in range(passsym):
chars += random.choice(charssym)
for p in range(passlen):
pwd += random.choice(chars)
print(pwd)
password()
pwd. And when I run the program and input my variables, the outputted password doesn't follow the criteria I had. for example I put 10 forpasslen, 3 forpasslet, and 3 forpassnum. So in theory I should receive a password with 3 letters, 3 numbers, and 4 symbols but I received 4##&$*c40# as my output.forloop and you don't care about the values from therange()you can use a single underscore as your variable name:_Your code useslet,num, andsymas the variable names for the loop values, but your code doesn't ever need to use them for anything. This isn't wrong, but using the_idiom is almost like putting in a comment saying "we don't care about the loop value here, we just want to loop a specific number of times".