Skip to content

Commit b803a53

Browse files
committed
improved overall script
1 parent 3030551 commit b803a53

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed
Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
1-
import string #String module will import all the nessary ascii character
2-
import random #random module help us to import functions needed to generate random element.
1+
import string
2+
import secrets
33

4-
passwrd = string.ascii_letters+string.digits+string.punctuation #This will generate a string consist of all ascii character.
4+
# Define the set of characters to be used in the password
5+
CHARACTER_SET = string.ascii_letters + string.digits + string.punctuation
56

6-
numPass = int(input("How many passwords do you need to be generated? "))
7-
length = int(input("Enter the length of the password(s): "))
7+
def generate_password(length):
8+
"""Generate a random password of the specified length."""
9+
password = ''.join(secrets.choice(CHARACTER_SET) for i in range(length))
10+
return password
811

9-
print("List(s) of Generated passwords: ")
12+
def main():
13+
# Prompt the user for the number of passwords to generate and their length
14+
while True:
15+
try:
16+
num_pass = int(input("How many passwords do you want to generate? "))
17+
password_length = int(input("Enter the length of the password(s): "))
18+
break
19+
except ValueError:
20+
print("Please enter a valid integer.")
1021

11-
for _ in range(numPass):
12-
print(''.join(random.sample(passwrd, k=length))) #sample() generates an array of random characters of length k
22+
# Generate the specified number of passwords and print them to the console
23+
print("Generated passwords:")
24+
for i in range(num_pass):
25+
password = generate_password(password_length)
26+
print(f"{i+1}. {password}")
27+
28+
if __name__ == "__main__":
29+
main()

0 commit comments

Comments
 (0)