Last active
April 16, 2023 09:21
-
-
Save Igaryu/6076f080bf388d7616dbb215210bfe66 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Why importing tons of functions, when I can import the only the 6 I need? | |
from sys import exit | |
from random import choice | |
from string import ascii_letters, digits, punctuation | |
# | |
# Using try - except trap to verify if input data is a number | |
# if not then exit with exit code -9 | |
# | |
try: | |
password_length=int(input('\nDigit the lentgh of password to generate: ')) | |
except: | |
print("\nA number whas required!!\n") | |
exit(-9) | |
# | |
# A faster way to concatenate strings is .join() the + method is really slow and deprecated | |
# | |
password_characters=''.join([ascii_letters,digits,punctuation]) | |
password = [] | |
for x in range(password_length): | |
password.append(choice(password_characters)) | |
# | |
# using the f-string is more python in printing in version 3 | |
# | |
print(f"\n{''.join(password)} \n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A lighter version could be obtained using a constant and not importing libraries like in this git:
https://gist.github.com/Igaryu/a0498c843a10f3d502d78a862bd7f2db