Home > OS >  How to create a choice based password generator in python?
How to create a choice based password generator in python?

Time:09-29

I am trying to make a password generator in python that

  1. Lets the user choose how long it should be
  2. Is able to include uppercase letters, lowercase letters, numbers, and symbols when asked if the user wants them
  3. Displays the final password.

I've been mulling over it for a while now, but I can't figure out what I'm doing wrong.

It's python 3.9.7 if that matters

import random
import string

uppercase = list(string.ascii_uppercase)
lowercase = list(string.ascii_lowercase)
numbers = list(string.digits)
symbols = list(string.punctuation)

password = ''

def generatePassword(passwordLength, useUppercase, useLowercase, useNumbers, useSymbols, password):
    charsToUse = []
    

    if useUppercase == True:
        charsToUse.extend(uppercase)
    if useLowercase == True:
        charsToUse.extend(lowercase)
    if useNumbers == True:
        charsToUse.extend(numbers)
    if useSymbols == True:
        charsToUse.extend(symbols)
    

    while passwordLength < password:
        password.append(random.choice(charsToUse))
    
    random.shuffle(password)
    
    return password


        

print('====================================================')
print('==== WELCOME TO THE ULTIMATE PASSWORD GENERATOR ====')
print('====================================================')

passwordLength = input('How long would you like the password to be?: ')

useUppercase = bool(input('Upercase letters? 1=Yes, Leave Blank=No: '))
useLowercase = bool(input('Lowercase letters? 1=Yes, Leave Blank=No: '))
useNumbers = bool(input('Numbers? 1=Yes, Leave Blank=No: '))
useSymbols = bool(input('Symbols? 1=Yes, Leave Blank=No: '))

print('----------------------------------------------------')
print('Generating password...')
print('----------------------------------------------------')
print("Your new password is: "   generatePassword(passwordLength, useUppercase, useLowercase, useNumbers, useSymbols, password))
print('====================================================')

CodePudding user response:

Some minor changes:

  1. Cast passwordLength to int
  2. random works on a list, not a str
  3. Use random.choices to generate a random sample of size passwordLength instead of looping.

Try it like this:

import random
import string

uppercase = list(string.ascii_uppercase)
lowercase = list(string.ascii_lowercase)
numbers = list(string.digits)
symbols = list(string.punctuation)

def generatePassword(passwordLength, useUppercase, useLowercase, useNumbers, useSymbols):
    password = list()
    charsToUse = []

    if useUppercase:
        charsToUse.extend(uppercase)
    if useLowercase:
        charsToUse.extend(lowercase)
    if useNumbers:
        charsToUse.extend(numbers)
    if useSymbols:
        charsToUse.extend(symbols)
    
    password = random.shuffle(random.choices(charsToUse, k=passwordLength))
    return "".join(password)

print('====================================================')
print('==== WELCOME TO THE ULTIMATE PASSWORD GENERATOR ====')
print('====================================================')

passwordLength = int(input('How long would you like the password to be?: '))

useUppercase = bool(input('Upercase letters? 1=Yes, Leave Blank=No: '))
useLowercase = bool(input('Lowercase letters? 1=Yes, Leave Blank=No: '))
useNumbers = bool(input('Numbers? 1=Yes, Leave Blank=No: '))
useSymbols = bool(input('Symbols? 1=Yes, Leave Blank=No: '))

print('----------------------------------------------------')
print('Generating password...')
print('----------------------------------------------------')
print("Your new password is: "   generatePassword(passwordLength, useUppercase, useLowercase, useNumbers, useSymbols))
print('====================================================')

CodePudding user response:

you can try:

import random
import string

uppercase = list(string.ascii_uppercase)
lowercase = list(string.ascii_lowercase)
numbers = list(string.digits)
symbols = list(string.punctuation)
password = []


def generatePassword(passwordLength, useUppercase, useLowercase, useNumbers, useSymbols, password):
    charsToUse = []
    if useUppercase == True:
        charsToUse.extend(uppercase)
    if useLowercase == True:
        charsToUse.extend(lowercase)
    if useNumbers == True:
        charsToUse.extend(numbers)
    if useSymbols == True:
        charsToUse.extend(symbols)
    

    while int(passwordLength) > len(password):
        password.append(random.choice(charsToUse))
    
    random.shuffle(password)
    
    return password

passwordLength = input('How long would you like the password to be?: ')
useUppercase = bool(input('Upercase letters? 1=Yes, Leave Blank=No: '))
useLowercase = bool(input('Lowercase letters? 1=Yes, Leave Blank=No: '))
useNumbers = bool(input('Numbers? 1=Yes, Leave Blank=No: '))
useSymbols = bool(input('Symbols? 1=Yes, Leave Blank=No: '))

Plist = generatePassword(passwordLength, useUppercase, useLowercase, useNumbers, useSymbols, password)
print(''.join(Plist))
  • Related