Python · June 25, 2023 0

Random Password Generator

import random
import pyperclip
print("*******************************")
print("** RANDOM PASSWORD GENERATOR **")
print("*******************************")
print("Welcome to Random Password Generator! You will get an auto-generated password.")
letters = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXVCBNM"
numbers = "1234567890"
specialcharacters = "!@#$%^&*!"
lettercount = 0
numbercount = 0
specialcharacterscount = 0
def testint(num):
    try:
        num = int(num)
        return num
    except ValueError:
        while True:
            try:
                num = int(input("Please enter an integer: "))
                return num
                break
            except ValueError:
                print("Invalid input. Please enter an integer.")
while lettercount + numbercount + specialcharacterscount < 10:
    print("The password should be at least 10 characters long and include at least 1 number and 1 special character.")
    lettercount = input("How many letters?  ")
    lettercount = testint(lettercount)
    numbercount = input("How many numbers?  ")
    numbercount = testint(numbercount)
    while numbercount == 0:
        numbercount = input("There must be at least one number. How many numbers?  ")
        numbercount = testint(numbercount)
    specialcharacterscount = input("How many special characters?  ")
    specialcharacterscount = testint(specialcharacterscount)
    while specialcharacterscount == 0:
        specialcharacterscount = input("There must be at least one special character. How many special characters?  ")
        specialcharacterscount = testint(specialcharacterscount)
password = ""
def add(count, type):
    global password
    if type == "s":
        for i in range(count):
            password += random.choice(list(specialcharacters))
    elif type == "n":
        for i in range(count):
            password += random.choice(list(numbers))
    else:
        for i in range(count):
            password += random.choice(list(letters))
add(specialcharacterscount, "s")
add(numbercount, "n")
add(lettercount, "l")
password = list(password)
random.shuffle(password)
password = "".join(password)
if password.lower() == password and lettercount > 1:
    for i in range(len(password)):
        if "qwertyuiopasdfghjklzxcvbmn".find(password[i]) != -1:
            password = list(password)
            password[i] = password[i].upper()
            password = "".join(password)
            break
if password.upper() == password and lettercount > 1:
    for i in range(len(password)):
        if "QWERTYUIOPASDFGHJKLZXCVBNM".find(password[i]) != -1:
            password = list(password)
            password[i] = password[i].lower()
            password = "".join(password)
            break
print("The Random Password Generater has created a high security password: {}".format(password))
pyperclip.copy(password)
print("This password has been copied to your clipboard, remember it!")

This program generates a random password.

  • The password is at least 10 characters long and includes at least 1 number and special character.
  • You can tell the program how many special characters, numbers, and letters you want.
  • The password has letters, numbers, and other characters randomly dispersed throughout. For example, it doesn’t always generate passwords in the form aaaaa###&& or a#&a#&aaaa.
  • Automatically copies the password to the user’s clipboard.
  • The code ensures that each password has at least 1 upper case and 1 lower case letter.