#Password Generator Project
import random
letters = ['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', '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']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', ' ']
print("Welcome to the PyPassword Generator!")
nr_letters= int(input("How many letters would you like in your password?\n"))
nr_symbols = int(input(f"How many symbols would you like?\n"))
nr_numbers = int(input(f"How many numbers would you like?\n"))
password = []
for l in range (0 , nr_letters 1):
random_l = random.choice(letters)
password.append(random_l)
for i in range (0 , nr_symbols 1):
random_i = random.choice(symbols)
password.append(random_i)
for n in range (0 , nr_numbers 1):
random_n = random.choice(numbers)
password.append(random_n)
print(random.choice(password))
I want to randomize and print the password list at the end of the code but it gives me only one character. When I print it without random function or shuffle it prints properly.
CodePudding user response:
In OP's question password is a list (a sequence). random.choice() selects a single pseudo-random item from a sequence. Thus you get just one character.
Using random.choices() simplifies the entire process
from random import choices, shuffle
from string import ascii_lowercase, digits
symbols = '!#$%&()* '
print("Welcome to the PyPassword Generator!")
nr_letters = int(input("How many letters would you like in your password? "))
nr_symbols = int(input("How many symbols would you like? "))
nr_numbers = int(input("How many numbers would you like? "))
password = choices(ascii_lowercase, k=nr_letters)
password = choices(digits, k=nr_numbers)
password = choices(symbols, k=nr_symbols)
shuffle(password)
print(''.join(password))
Example:
Welcome to the PyPassword Generator!
How many letters would you like in your password? 6
How many symbols would you like? 2
How many numbers would you like? 2
bf%t6vf(g0
CodePudding user response:
random.choice(password)
just picks a random item from password
. Use random.shuffle(password)
to shuffle it.
random.shuffle(password)
print("".join(password))
CodePudding user response:
Changes made
(1) you are taking one character extra in each of them.. Corrected!
(2) created a variable res_in_str which is string storing the password generated
(3) created a variable res_in_list which is list storing the password generated in list format
Code:-
import random
letters = ['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', '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']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', ' ']
print("Welcome to the PyPassword Generator!")
nr_letters= int(input("How many letters would you like in your password?\n"))
nr_symbols = int(input(f"How many symbols would you like?\n"))
nr_numbers = int(input(f"How many numbers would you like?\n"))
password = []
for l in range (0 , nr_letters):
random_l = random.choice(letters)
password.append(random_l)
for i in range (0 , nr_symbols):
random_i = random.choice(symbols)
password.append(random_i)
for n in range (0 , nr_numbers):
random_n = random.choice(numbers)
password.append(random_n)
res_in_str=""
res_in_list=[]
while len(password):
char=random.choice(password)
password.remove(char)
res_in_str =char
res_in_list.append(char)
print("Password Generated: ",res_in_str)
print("Password Generated in list format: ",res_in_list)
Output:-
Welcome to the PyPassword Generator!
How many letters would you like in your password?
6
How many symbols would you like?
3
How many numbers would you like?
3
Password Generated: ZIL!0cy#r25#
Password Generated in list format: ['Z', 'I', 'L', '!', '0', 'c', 'y', '#', 'r', '2', '5', '#']
CodePudding user response:
This is how I would solve it:
from string import ascii_letters, digits, punctuation
from random import choice, shuffle
def selector(quantity, type):
return(return([choice([x for x in type]) for _ in range(0, quantity)]))
print("Welcome to the PyPassword Generator!")
nr_letters= selector(int(input("How many letters would you like in your password?\n")), ascii_letters)
nr_symbols = selector(int(input(f"How many symbols would you like?\n")), punctuation)
nr_numbers = selector(int(input(f"How many numbers would you like?\n")), digits)
temp_password = nr_letters nr_symbols nr_numbers
shuffle(temp_password)
password = "".join(temp_password)
print(password)
Results:
Welcome to the PyPassword Generator!
How many letters would you like in your password?
8
How many symbols would you like?
4
How many numbers would you like?
2
1IEa>L'cBSR_^9
Welcome to the PyPassword Generator!
How many letters would you like in your password?
8
How many symbols would you like?
0
How many numbers would you like?
6
bJOV06DR340Y4S
CodePudding user response:
Your code
Your code is good until the last row in the print
function, you could use the join
function instead like this :
print(password)
# ['s', 'F', 'h', 'i', 'b', 'f', '*', '$', '(', '&', '(', '$', '4', '4', '6', '2', '4', '2']
# So you could use for instance the join function combined with
# random to reconstitue a readable randomized password
print(''.join(random.sample(password, k=len(password))))
# s$($b4(24&2Fhf64*i
The fact is that, by using the choice
function in the print
row, you ask for returning a unique randomized letter from your password.
My proposal
However, I proposed you the following code instead :
from random import sample
from string import ascii_uppercase, digits
symbols = '!#$%&()* ' # or string.punctuation instead symbols
nr_letters= int(input("How many letters would you like in your password?\n"))
nr_symbols = int(input(f"How many symbols would you like?\n"))
nr_numbers = int(input(f"How many numbers would you like?\n"))
agg_s = sample(ascii_uppercase, nr_letters) sample(symbols, nr_symbols) sample(digits, nr_numbers)
password = ''.join(sample(agg_s, k=len(agg_s)))
print("password : " , password)
# password : 9E6XC%P&$24J1(!
Note :
Taking a same length randomized sample from a sample is equivalent to a randomized permutation.
So no matter if the variable agg_s = 5 letters 5 symbols 5 digits, because after using of sample(k, len(k))
, you effectively have a randomized password.