Issue
I’m taking a 100 Days of Code in Python, and I’m trying to create a Python password generator by taking in user input for how many letters, numbers, and symbols they’d like in their password.
The program below runs and generates the desired output, but I know there must be a better way than iterating over the range an arbitrary number of times to generate a fixed-length password.
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?: '))
nr_symbols = int(input('How many symbols would you like?: '))
nr_numbers = int(input('How many numbers would you like?: '))
password = ""
# Generate unshuffled password
# for i in range(1, (nr_letters + 1)):
# password += random.choice(letters)
# for i in range(1, (nr_symbols + 1)):
# password += random.choice(numbers)
# for i in range(1, (nr_numbers +1)):
# password += random.choice(symbols)
# print(password)
letter_counter = 0
symbol_counter = 0
number_counter = 0
# NOTE: This seems dumb but it works so...
for i in range(0, 100):
random_int = random.randint(0, 2)
if random_int == 0 and letter_counter < nr_letters:
password += random.choice(letters)
letter_counter += 1
elif random_int == 1 and symbol_counter < nr_symbols:
password += random.choice(symbols)
symbol_counter += 1
elif random_int == 2 and number_counter < nr_numbers:
password += random.choice(numbers)
number_counter += 1
print(password)
Is there a cleaner way I can create a shuffled, fixed-length string through a Python for loop?
For the future, is there a major downside to iterating through a loop more times than it takes to generate the desired output?
Solution
I think this is what you are looking for?:
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?: '))
nr_symbols = int(input('How many symbols would you like?: '))
nr_numbers = int(input('How many numbers would you like?: '))
password = ""
for i in range(1, sum([nr_letters,nr_numbers,nr_symbols])+1):
if i<= nr_letters:
password += random.choice(letters)
elif i > nr_letters and i<=nr_symbols+nr_letters:
password += random.choice(symbols)
elif i > nr_symbols+nr_letters:
password += random.choice(numbers)
password=[x for x in password]
random.shuffle(password)
password="".join(password)
print(password)
Basically, instead of iterating through an arbitrary number, it adds up the number of characters required, then it goes through the range, and adding the numbers/symbols/letters when the requirements are met.
The requirements are basic. While below or equal to the number of letters required, add a letter. Then, you go and add the number of letters and symbols together, and say that it has to meet both above the number of letters, and below letters+symbols. Then, if i
is above letters+symbols, add numbers. That’s pretty much the pattern.
The downside is that you will have to shuffle afterwards like I’ve done above. password=[x for x in password]
basically turns password
into a list. Then you shuffle it with random.shuffle(password)
, and then join it password="".join(password)
.
It’s basically your first commented out iterations bundled up in 1.
Answered By – Agent Biscutt
Answer Checked By – Katrina (BugsFixing Volunteer)