如何在python中创建基于选项的密码生成器?

2024-06-12 04:14:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用python制作一个密码生成器

  1. 让用户选择它的长度
  2. 当询问用户是否需要时,能够包括大写字母、小写字母、数字和符号
  3. 显示最终密码

我已经考虑了一段时间了,但我不知道我做错了什么

如果这很重要的话,它就是Python3.9.7

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('====================================================')


Tags: trueinputstringifpasswordlistboolprint
2条回答

您可以尝试:

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))

一些小改动:

  1. 将passwordLength强制转换为int
  2. randomlist上工作,而不是在str上工作
  3. 使用random.choices生成大小为passwordLength的随机样本,而不是循环

试着这样做:

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('====================================================')

相关问题 更多 >