I made a password generator that creates a random set of numbers, letters, and symbols, but the results are always printed in that order. Each character type is created through its own while loop and I'm wondering if there is a way to for the while loops to execute at random rather than in a top down order. Thank you!
import random
alphabet = 'abcdefghijklmnopqrstuvwxyz'
alphabet_up = alphabet.upper()
numz = '0123456789'
symz = '~@!#$%&*'
alpha_character_type = [alphabet, alphabet_up]
index_counter_num = 0
index_counter_letter = 0
index_counter_symz = 0
print('Welcome to password generator')
pw_length = int(input('Please enter password length: '))
if pw_length < 6:
pw_length = int(input('Password must be at least 6 characters long. Please enter a larger number: '))
letters_num = int(input('Enter desired number of letters: '))
numbers_num = int(input('Enter desired number of numbers: '))
if letters_num + numbers_num>= pw_length:
print('Please leave room for symbols. Reduce the amount of letters and/or numbers')
letters_num = int(input('Enter new desired number of letters: '))
numbers_num = int(input('Enter new desired number of numbers: '))
characters_left = pw_length - letters_num - numbers_num
print('Thank you. There is now', characters_left, 'spaces left for symbols. Your password will now be generated' )
print('Here is your password: ')
#NUMBERS LOOP
while index_counter_num < numbers_num:
selected_numz = random.choice(numz)
index_counter_num += 1
print(selected_numz, end= '')
#LETTERS LOOP
while index_counter_letter < letters_num:
style = random.choice(alpha_character_type)
style_character = random.choice(style)
selected_letters = style_character
index_counter_letter += 1
print(selected_letters, end= '')
# SYMBOLS LOOP
characters_left = pw_length - letters_num - numbers_num
while index_counter_symz < characters_left:
selected_symz = random.choice(symz)
index_counter_symz += 1
print(selected_symz, end = '')
Aucun commentaire:
Enregistrer un commentaire