Related
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()
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 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 need to assign the uppercase to numbers in a dictionary but with one letter S not there.
ie.
in this alphadict = dict((x, i) for i, x in enumerate(string.ascii_uppercase)) I have currently all the alphabets of the dictionary.
What is the simplest way to delete the entry for S and shift rest of the values to the left.
If there is some other way to create this dictionary do tell.....
I am also in need of this..... Now I get a number from user..... This number in the dictionary should be assigned S and all the other dictionary items can be readjusted....
ie say the user gives me 3 the dictionary should look like
0- A
1- B
2- C
3- S
and rest follow from D to Z without S.......
Please help..... Thanks a lot....
The simplest way is to remove the letter 'S' before you create the dictionary.
Use string.ascii_uppercase.replace('S', '') instead of string.ascii_uppercase.
alphadict = dict((x, i) for i, x in enumerate(string.ascii_uppercase) if x != 'S')
If I understand you properly, the simplest thing to do seems like it would be to first create a list with the letters in the order you want, and then convert that into a dictionary:
import string
sans_S = [c for c in string.ascii_uppercase if c is not 'S']
user_choice = 3
alphabet = sans_S[0:user_choice] + ['S'] + sans_S[user_choice:]
print alphabet
# ['A', 'B', 'C', 'S', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
# 'O', 'P', 'Q', 'R', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
# now create dictionary using modified list
alphadict = dict((x, i) for i, x in enumerate(alphabet))
# print out alphadict sorted by item values (not a necessary part of answer)
revdict = dict( (v,k) for k,v in alphadict.iteritems() )
print '{',
for v in sorted(alphadict.itervalues()):
print "%r:%2d," % (revdict[v], v),
print '}'
# { 'A': 0, 'B': 1, 'C': 2, 'S': 3, 'D': 4, 'E': 5, 'F': 6, 'G': 7, 'H': 8,
# 'I': 9, 'J':10, 'K':11, 'L':12, 'M':13, 'N':14, 'O':15, 'P':16, 'Q':17,
# 'R':18, 'T':19, 'U':20, 'V':21, 'W':22, 'X':23, 'Y':24, 'Z':25, }
for Q2
def makealphabet(i):
a=list(string.ascii_uppercase)
a[i:i]=a.pop(a.index('S'))
return ''.join(a)
If you want to do operations like the one in your update to the question, I would store the data in a list instead of a dictionary right from the beginning:
l = list(string.ascii_uppercase)
l.remove('S')
print l
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
l.insert(3, 'S')
print l
['A', 'B', 'C', 'S', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
The letters can now be accessed by there indices just as if they were in a dictionary.