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()
Related
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'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)
I want this script to type out every possible combination of letters, numbers and symbols, even if it would take months to do. Can anyone help?
lower_a = ['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 = ['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']
num = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
all = lower_a + upper_a + num
def recursive_product(myList, length, myString = ""):
if length == 0:
print (myString)
return
for c in myList:
recursive_product(myList, length-1, myString + c)
for r in range(1, 3):
recursive_product(all, r)
I found this code from somewhere a little while ago and I want to get it to work with every combination.
You can do it iterative:
lower_a = ['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 = ['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']
num = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
for a in lower_a:
print(a)
for b in upper_a:
print(a + b)
print(b)
for c in num:
print(a + c)
print(c + b)
print(a + b + c)
print(c)
Here is one way you can do it:
lower_a = ['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 = ['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']
num = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
all = lower_a + upper_a + num
def words(combinations,length,prefix =""):
if(len(prefix)>=length):
return prefix
for i in range(len(combinations)):
print(words(combinations,length, prefix+all[i]))
running words(all,3) would print all possible 3 character combinations
If you're looking for the cartesian product of all the combinations (e.g. aA0 to zZ9),
import itertools
import string
for x in itertools.product(string.ascii_lowercase, string.ascii_uppercase, string.digits):
print(''.join(x))
If you're looking for all the possible combinations of length 3,
import itertools
import string
for x in itertools.combinations(string.ascii_lowercase + string.ascii_uppercase + string.digits, 3):
print(''.join(x))
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
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']