Reputation: 1
I've tried to create a random password generator with Python but the output returned blank. I'm new to Python and, therefore, can't see where I made a mistake.
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"))
characters = [letters, symbols, numbers]
password = ""
#Password within the boundaries
letter_count = 0
symbol_count = 0
number_count = 0
for char in characters[random.randint(0, 2)]:
if letters == char and letter_count <= nr_letters:
password += letters[random.randint(0, 47)]
letter_count += 1
elif symbols == char and symbol_count <= nr_symbols:
password += symbols[random.randint(0, 8)]
symbol_count += 1
elif numbers == char and number_count <= nr_numbers:
password += numbers[random.randint(0, 9)]
number_count += 1
if len(password) == nr_letters + nr_symbols + nr_numbers:
break
print(password)
Upvotes: 0
Views: 107
Reputation: 2967
The problem is that when you are not comparing the same "category" of things.
The line characters[random.randint(0, 2)]:
will return you either one of the list of letters
/ numbers
/ symbols
.
So for the loop for char in characters[random.randint(0, 2)]:
, char
is actually looping through either one of the list, instead of the whole list that you intended. And therefore, char
will never equal to letters
/ numbers
/ symbols
.
What you can do to fix it is to change this loop into a while-loop, where the condition is that when the length of the password does not exceed the user input, keep adding a character from a random selection of the list.
while len(password) != nr_letters + nr_symbols + nr_numbers:
selected_list = characters[random.randint(0, 2)]
if letters == selected_list and letter_count < nr_letters:
password += letters[random.randint(0, 47)]
letter_count += 1
elif symbols == selected_list and symbol_count < nr_symbols:
password += symbols[random.randint(0, 8)]
symbol_count += 1
elif numbers == selected_list and number_count < nr_numbers:
password += numbers[random.randint(0, 9)]
number_count += 1
print(password)
Upvotes: 1
Reputation: 83
I guess there's something wrong with your logic around the for loop, a while loop seems better in this situation. your comparison for char
to a list item was causing issues. Hope my solution is understandable and helps.
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"))
characters = [letters, symbols, numbers]
password = ""
# Password within the boundaries
letter_count = 0
symbol_count = 0
number_count = 0
while len(password) != nr_letters + nr_symbols + nr_numbers:
number = random.randint(0,2)
if characters[number] == letters and letter_count <= nr_letters:
password += letters[random.randint(0, 46)]
letter_count += 1
elif characters[number] == symbols and symbol_count <= nr_symbols:
password += symbols[random.randint(0, 8)]
symbol_count += 1
elif characters[number] == numbers and number_count <= nr_numbers:
password += numbers[random.randint(0, 9)]
number_count += 1
print(password)
Upvotes: 1
Reputation: 11
As said before, you are comparing list (letters) to a single character (char). Instead you should ask if character is in the list. Try writing:
if char in letters
Upvotes: 1