Password generator returns blank result - python

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)

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

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)

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)

Related

How to Loop a Function in Python Without a Clear Variable

Below is code that correctly outputs a random password based on the sequence, using multiple for loops. I want to loop the function 10 times to output 10 passwords, but I can't seem to figure out what variable to loop in this case.
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!")
num_letters= random.randint(3, 6)
num_numbers = random.randint(1, 4)
num_symbols = (10 - num_letters - num_numbers)
print(f'Your requested {num_letters} letters, {num_numbers} numbers, and {num_symbols} symbols:')
password_list = []
for num in range(num_letters):
`letter = random.choice(letters)`
`password_list.append(letter)`
for num in range(num_numbers):
`number = random.choice(numbers)`
`password_list.append(number)`
for num in range(num_symbols):
`symbol = random.choice(symbols)`
`password_list.append(symbol)`
random.shuffle(password_list)
password = ''
for char in password_list:
`password = password + char`
print(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 = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
def generator():
print("Welcome to the PyPassword Generator!")
num_letters= random.randint(3, 6)
num_numbers = random.randint(1, 4)
num_symbols = (10 - num_letters - num_numbers)
print(f'Your requested {num_letters} letters, {num_numbers} numbers, and {num_symbols} symbols:')
password_list = []
for num in range(num_letters):
letter = random.choice(letters)
password_list.append(letter)
for num in range(num_numbers):
number = random.choice(numbers)
password_list.append(number)
for num in range(num_symbols):
symbol = random.choice(symbols)
password_list.append(symbol)
random.shuffle(password_list)
password = ''
for char in password_list:
password = password + char
print(password)
return password
print([generator() for i in range(10)])
Just put this generator in a function!
As such I have a main function to run the program and ask how many times you would like to iterate the password generator function.
import random
def main():
print("Welcome to the PyPassword Generator!")
val = input("Enter number of passwords you want to generate: ")
itterate = int(val)
#print("Generated passwords: ")
for i in range(itterate):
passwordGenerator()
def passwordGenerator():
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 = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
num_letters= random.randint(3, 6)
num_numbers = random.randint(1, 4)
num_symbols = ((10 - num_letters - num_numbers) + 1)
print(f'Your requested {num_letters} letters, {num_numbers} numbers, and {num_symbols} symbols:')
password_list = []
for num in range(num_letters):
letter = random.choice(letters)
password_list.append(letter)
for num in range(num_numbers):
number = random.choice(numbers)
password_list.append(number)
for num in range(num_symbols):
symbol = random.choice(symbols)
password_list.append(symbol)
random.shuffle(password_list)
password = ''
for char in password_list:
password = password + char
print(password)
if __name__ == "__main__":
main()
Sample output:
Welcome to the PyPassword Generator!
Enter number of passwords you want to generate: 3
Your requested 3 letters, 3 numbers, and 5 symbols:
(7I3$7&%)vm
Your requested 4 letters, 1 numbers, and 6 symbols:
&*PV#6u#N&)
Your requested 3 letters, 2 numbers, and 6 symbols:
8#*)8!!lt)h
I put all your loops in the function to make your code easy to read and undrestand
no_passwords = 10 i make new variable to specify the number of passwords you want
for _ in range(no_passwords): generat_password() make loop run in range of the variable we made called no_passwords and each time the code will call the function generat_password() to generate a new 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 = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
def generat_password():
password_list = []
for num in range(num_letters):
letter = random.choice(letters)
password_list.append(letter)
for num in range(num_numbers):
number = random.choice(numbers)
password_list.append(number)
for num in range(num_symbols):
symbol = random.choice(symbols)
password_list.append(symbol)
random.shuffle(password_list)
password = ''
for char in password_list:
password = password + char
print(password)
print("Welcome to the PyPassword Generator!")
num_letters= random.randint(3, 6)
num_numbers = random.randint(1, 4)
num_symbols = (10 - num_letters - num_numbers)
no_passwords = 10
print(f'Your requested {num_letters} letters, {num_numbers} numbers, and {num_symbols} symbols:')
for _ in range(no_passwords):
generat_password()

Password generator output/ repetition

I've got two questions about the code included below. One I'm curious how do I prevent the code from putting the same number/symbol/letter(upper/lower) beside itself? for example P#ssword I'd like to make it so it wont output (P#S$word, P#sSword, P#$$word).
Secondly I'm hoping someone can help confirm what/why the output for the code is . . .
#uI7c
cbI#7u
cbl#7Iu
7Ilbn#cu
Icbnl#7nu
u7bclnnI#I
nlu#cnIU7Ib
7c#9unnlIIUb
I'm under the impression it has to do with lines(32,33,38). it appears to be showing me it shuffling in each letter/number/symbol up to the MAX_LEN
import random
import array
#adjust based on length required
MAX_LEN = 12
DIGITS = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
SYMBOLS = ['!', '#', '#', '$', '%', '^', '&', '*', '(', ')']
LOWER = ['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']
UPPER = ['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']
#Combines the above list into an array
list_array = DIGITS + SYMBOLS + UPPER + LOWER
#randomly select atleast 1 character from each
rand_digit = random.choice(DIGITS)
rand_symbol = random.choice(SYMBOLS)
rand_upper = random.choice(UPPER)
rand_lower = random.choice(LOWER)
#combine Char randomly, only contains 4 char at this point in code
temp_pass = rand_digit + rand_symbol + rand_upper + rand_lower
#1 from each now fill the rest of MAX_LEN by selecting randomly
for x in range(MAX_LEN - 4):
temp_pass = temp_pass + random.choice(list_array)
#convert temp password into array and shuffle to prevent
#consistent patterns/ begining of PW is predictable
temp_pass_list = array.array('u', temp_pass)
random.shuffle(temp_pass_list)
#traverse temp PW array and append char to form PW
password = ""
for x in temp_pass_list:
password = password + x
#print Password
print(password)
Thanks in advance!

I'm trying to make a password generetor using Python 3.7. I searched online how to use sample() but for me it doesn't work

So i want it to pick a random digit from a list 2 times nd then mix it together in order to get a password. I wanted the ready_password to print a list and it works but, it also prints [] and ''. So i decided to make it a tuple but i don't know how to mix a tuple. This is the error that I'm getting:
TypeError: sample() missing 1 required positional argument: 'k'
code-
import random
lower_case = ['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']
upper_case = ['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 = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
punctuation = ['!', '#', '#', '$', '%', '^', '&', '*', '(']
lower1 = random.choice(lower_case)
lower2 = random.choice(lower_case)
upper1 = random.choice(upper_case)
upper2 = random.choice(upper_case)
number1 = random.choice(numbers)
number2 = random.choice(numbers)
punctuation1 = random.choice(punctuation)
punctuation2 = random.choice(punctuation)
password_not_ready = (lower1 + lower2 + upper1 + upper2 + number1 + number2 + punctuation1 +
punctuation2)
ready_password = random.sample(password_not_ready)
print(ready_password)
You might want to use random.shuffle() to mix up the characters instead of having them in order:
from random import sample as sp
from random import shuffle as sh
lower_case = 'abcdefghijklmnopqrstuvwxyz'
upper_case = lower_case.upper()
numbers = '1234567890'
punctuation = '!##$%^&*('
ready_password = sp(lower_case,2)+sp(upper_case,2)+sp(punctuation,2)+sp(numbers,2)
sh(ready_password)
print(''.join(ready_password))
Output:
c3D#0hV%
sample() takes 2 arguments, the second being the length of the list to return, however I think what you meant to do was use shuffle(), to reorder all the randomly picked characters (sample will not shuffle the list). Change this line
ready_password = random.sample(password_not_ready)
to
ready_password = random.shuffle(password_not_ready, len(password_not_ready))
See the documentation for shuffle and for sample for more information.
K=8 for 8 digit password
import random
lower_case = ['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']
upper_case = ['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 = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
punctuation = ['!', '#', '#', '$', '%', '^', '&', '*', '(']
lower1 = random.choice(lower_case)
lower2 = random.choice(lower_case)
upper1 = random.choice(upper_case)
upper2 = random.choice(upper_case)
number1 = random.choice(numbers)
number2 = random.choice(numbers)
punctuation1 = random.choice(punctuation)
punctuation2 = random.choice(punctuation)
password_not_ready = (lower1 + lower2 + upper1 + upper2 + number1 + number2 + punctuation1 + punctuation2)
ready_password = random.sample(password_not_ready, k=8)
print(ready_password)
As per your requirement-
if you want to use random.sample you need to pass the second param k (length of the random sample from the password_not_ready.
so using
ready_password = random.sample(password_not_ready, k) #k is the desire length of the sample
print(ready_password)
with your existing code should work.
Links - https://www.geeksforgeeks.org/python-random-sample-function/
https://docs.python.org/3/library/random.html

Combination of lists in python?

I am writing a python program which has lists:
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', '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', 'X', 'Y', 'Z' ]
digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9' ]
How can I create a list or something so that my selection statement can identify
Identifiers = Letter { Letter | Digit }
Integers = Digit { Digit }
Any help would be appreciated.
Python strings have built in checks for letters and digits:
s = "test1"
for char in s:
is_digit = char.isdigit()
is_letter = char.isalpha() or is_digit
print char, is_letter, is_digit
If you are attempting to determine if a character is a letter or digit you should use this.
Since to qualify as an integer the entire item must be ints we can use .isdigit() if item passes this test we can append to integers if it fails we can append to identifiers
List comprehension:
identifiers = [i for i in l if not i.isdigit()]
integers = [i for i in l if i.isdigit()]
Expanded:
l = ['Aa', 'a3', '12']
identifiers = []
integers = []
for i in l:
if i.isdigit():
integers.append(i)
else:
identifiers.append(i)
print(identifiers)
print(integers)
['Aa', 'a3']
['12']

combination maker for lists of different length with nested for loops and not intertools

So I'm trying to get a handle on nested for loops in python, so i'm making a combination maker that loops until it finds the right combination of letters, numbers, and special characters. I know that intertools is the easiest way to do this, but I need to get a better understanding of how the nested for loops work. Here is how I would like the code to function.
a
b
c
...
1
2
3
...
~
#
#
...
A
B
C
...
aa
ab
ac
so on and so forth.
it continues to go through the lists I have until the correct set of characters. it will end up being quite a number of iterations by the end of it.
Here is my code
# this program is a password cracker that uses loops to find an appropriate character.
# It is capable of using numbers, letters, basic symbols, capitol letters, and up to 8 characters.
# It checks the whole list at once against the other whole list
lowalpha = ['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']
upalpha = ['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']
special = ['!', '#', '#', '$', '%', '^', '&', '*', '~', '?']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
hiddenpass = ['D', 'r', 'i', 'n', 'k', 'c', 'o', 'f', 'f', 'e', 'e']
i = len(hiddenpass)
password = ['']*i
low = 0
up = 0
sp = 0
num = 0
X = 0
Y = 0
Z = 1
def iterator():
global low, up, sp, num, X, password, lowalpha, upalpha, special, numbers
for low in range(len(lowalpha)):
password[X] = lowalpha[low]
for up in range(len(upalpha)):
password[X] = upalpha[up]
for sp in range(len(special)):
password[X] = special[sp]
for num in range(len(numbers)):
password[X] = numbers[num]
if password != hiddenpass:
for Y in range(len(password)):
for Z in range(len(lowalpha)):
password[Y] = lowalpha[Z]
iterator()
X = X + 1
low = 0
up = 0
sp = 0
num = 0
X = 0
Y = 0
Z = 1
for Z in range(len(upalpha)):
password[Y] = upalpha[Z]
iterator()
X = X + 1
low = 0
up = 0
sp = 0
num = 0
X = 0
Y = 0
Z = 1
for Z in range(len(special)):
password[Y] = special[Z]
iterator()
X = X + 1
low = 0
up = 0
sp = 0
num = 0
X = 0
Y = 0
Z = 1
for Z in range(len(numbers)):
password[Y] = numbers[Z]
iterator()
X = X + 1
else:
print('got em')
As of right now this does not work and fails the first time iterator() is called. it fails at password[X] = lowalpha[low] inside that function. https://imgur.com/a/4NZli Here is the error code.
I can't test with all combinations due to my processor heating issue but i can give you little idea what you are looking for :
import sys
lowalpha = ['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']
upalpha = ['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']
def repeat(*args, repeat=3):
result_1 = [tuple(i) for i in args] * repeat
final_list=[[]]
for i in result_1:
copy_final=final_list
final_list=[]
for j in copy_final:
for k in i:
find=([k]+j)
if find==['a','n','k']:
print("Here is find",find)
sys.exit()
final_list.append(find)
repeat(lowalpha,lowalpha)

Categories