I created a small script for generating password in python:
# LIBRARY IMPORTS
from datetime import datetime
import random
# VARIABLES
date = datetime.now()
dateFormat = str(date.strftime("%d-%m-%Y %H:%M:%S"))
lowerCase = "abcdefghijklmnopqrstuvwxyz"
upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
numbers = "0123456789"
symbols = "!?%&@#+*/()=<>-_"
passwordConstructor = lowerCase + upperCase + numbers + symbols
minPasswordLength: int = 8
maxPasswordLength: int = 20
# FUNCTIONS
def getUsername():
global userName
userName = str(input("Enter Username:"))
def getPasswordLength():
global passwordLength
passwordLength = input("Enter the length of password: ")
def generatePassword():
global password
password = "".join([random.choice(passwordConstructor) for i in range(passwordLength)])
print("1." + password)
password = ''.join(random.sample(password,len(password)))
print("2." + password)
password = ''.join(random.sample(password, len(password)))
print("3." + password)
def generateTextFile():
if userName != "":
f = open(userName.upper() + " - " + dateFormat + ".txt", "w+")
f.write("USERNAME: " + userName + "\nPASSWORD: " + password + "\n\nGENERATED ON: " + dateFormat)
else:
f = open("Password generated on " + dateFormat + ".txt", "w+")
f.write("PASSWORD: " + password + "\n\nGENERATED ON: " + dateFormat)
f.close()
def printPassword():
generatePassword()
print(password)
if getPasswordLength() == '':
print("Please enter a value. This cannot be empty.")
else:
if not getPasswordLength().isdigit():
print("Length of password must be a number.")
else:
if getPasswordLength() > maxPasswordLength:
print('Length of password is limited to ' + maxPasswordLength)
elif getPasswordLength() < minPasswordLength:
print('Length of password must be grater than ' + minPasswordLength)
else:
generatePassword()
But condition doesn't work and end up in an error. What I am doing wrong?
Conditions for User Input which should be covered:
- Cannot be empty.
- Must be number.
- Greater than minPasswordLength (8).
- Smaller than maxPasswordLength (20).