[英]Python Password Generator
I am trying to create a password generator in Python that must contain an uppercase letter, lowercase letter, and a number, and must have a length between 6 and 20 characters.我正在尝试在 Python 中创建一个密码生成器,它必须包含一个大写字母、小写字母和一个数字,并且长度必须在 6 到 20 个字符之间。
import random
import string
def password_gen():
while True:
length = random.randint(6,20)
pwd = []
for i in range(length):
prob = random.random()
if prob < 0.34:
char = random.choice(string.ascii_lowercase)
pwd.append(char)
elif prob < 0.67:
char = random.choice(string.ascii_uppercase)
pwd.append(char)
else:
char = str(random.randint(0,9))
pwd.append(char)
pwd = ''.join(pwd)
#check password here
return pwd
However, I am having trouble checking the password to make sure it contains the required characters listed earlier.但是,我无法检查密码以确保它包含前面列出的所需字符。 I am not sure if/how i would use a continue statement.Any help would be greatly appreciated!
我不确定我是否/如何使用 continue 语句。任何帮助将不胜感激!
I think this would be a bit easier to ensure you meet the base requirements if you just handle those upfront.我认为如果您只是预先处理这些,这将更容易确保您满足基本要求。
import random
import string
def generate_pw():
length = random.randint(6,20) - 3
pwd = []
pwd.append(random.choice(string.ascii_lowercase))
pwd.append(random.choice(string.ascii_uppercase))
pwd.append(str(random.randint(0,9)))
# fill out the rest of the characters
# using whatever algorithm you want
# for the next "length" characters
random.shuffle(pwd)
return ''.join(pwd)
This will ensure your password has the characters you need.这将确保您的密码包含您需要的字符。 For the rest of the characters you could for example just use a list of all alphanumeric characters and call
random.choice
length
times.例如,对于其余字符,您可以只使用所有字母数字字符的列表并调用
random.choice
length
时间。
you can use isupper() and islower() functions to get does your password contain uppercase and lowercase.您可以使用 isupper() 和 islower() 函数来获取您的密码是否包含大写和小写。
eg例如
upper=0
lower=0
for i in range(length):
if (pwd[i].islower()):
upper=1
elif (pwd[i].isupper()):
lower=1
import random
import string
def password_gen():
lower_case_letter = random.choice(string.ascii_lowercase)
upper_case_letter = random.choice(string.ascii_uppercase)
number = random.choice(string.digits)
other_characters = [
random.choice(string.ascii_letters + string.digits)
for index in range(random.randint(3, 17))
]
all_together = [lower_case_letter, upper_case_letter] + other_characters
random.shuffle(all_together)
return ''.join(all_together)
Password Generator more broken down, you can get any number you wish, but it outputs a pattern by first adding letters, then numbers and finally symbols
密码生成器更细分,你可以得到任何你想要的数字,但它输出一个模式,首先添加字母,然后是数字,最后是符号
import random
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
print("Welcome to the PyPassword Generator!")
nr_letters= int(input("How many letters would you like in your password?\n"))
nr_symbols = int(input(f"How many symbols would you like?\n"))
nr_numbers = int(input(f"How many numbers would you like?\n"))
passwordlength = nr_letters + nr_numbers + nr_symbols
chars = ""
for x in range (0, nr_letters):
char = random.choice(letters)
chars += char
nums = ""
for y in range (0, nr_numbers):
num = random.choice(numbers)
nums+=num
syms = "" # string accumulator
for z in range (0, nr_symbols):
symb = random.choice(symbols)
syms += symb
print(f"Here is your password: {chars}{nums}{syms}")
import random
import time
Uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Lowercase = "abcdefghijklmnopqrstuvwxyz"
Digits = "0123456789"
Symbols = "()!?*+=_-"
Mixedbag = Uppercase + Lowercase + Digits + Symbols
#This function allwos the user to generate a random password containing x
amount of uppercase, lowercase and symbols to form a password
def generate():
print ("Starting the word generating process")
time.sleep(1)
while True:
word_length = random.randint(8, 20)
Madeword = ""
for x in range(word_length):
ch = random.choice(Mixedbag)
Madeword = Madeword + ch
break
print ("Your word is ", Madeword)
import re
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.