我正在尝试用python制作一个密码生成器
我已经考虑了一段时间了,但我不知道我做错了什么
如果这很重要的话,它就是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('====================================================')
您可以尝试:
一些小改动:
int
random
在list
上工作,而不是在str
上工作random.choices
生成大小为passwordLength
的随机样本,而不是循环李>试着这样做:
相关问题 更多 >
编程相关推荐