File tree Expand file tree Collapse file tree 1 file changed +25
-8
lines changed
PASSWORD RELATED/RandomPassword Expand file tree Collapse file tree 1 file changed +25
-8
lines changed Original file line number Diff line number Diff line change 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
3
3
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
5
6
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
8
11
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." )
10
21
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 ()
You can’t perform that action at this time.
0 commit comments