Skip to content

Commit 813e05e

Browse files
authored
Improvement of the password generator
Indeed, the previous implementation could not handle big length. I love #Hacktoberfest :-) What about you?
1 parent 778703d commit 813e05e

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

Password_Generator/generate.py

+16-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1-
import random
2-
import string
1+
from random import choice
2+
from string import printable
33

44

5-
def randompassword(number):
6-
chars = random.sample(string.ascii_lowercase + string.digits, number)
7-
passw = ''.join(map(str, chars))
8-
return passw
5+
def random_password(length):
6+
"""
7+
Provides a random password of the given length.
98
9+
:param int length: The length of the password to generate.
10+
"""
1011

11-
amount = int(input("How many passwords:\n"))
12-
number = int(input("Lenght of password?\n"))
13-
for i in range(amount):
14-
print(f" Password: {i} - {randompassword(number)} ")
12+
return "".join([choice(printable) for x in range(int(length))])
13+
14+
15+
if __name__ == "__main__":
16+
amount = int(input("How many passwords: "))
17+
number = int(input("Length of password? "))
18+
19+
for i in range(1, amount + 1):
20+
print(f" Password: {i} - {repr(random_password(number))} ")

0 commit comments

Comments
 (0)