我正在尝试创建密码生成器。我已经弄清楚了如何确保密码包含数字,大写和小写字符。密码是通过string.printable
函数随机生成的。
这是string.printable
的一部分,我将其用作密码中的可接受字符:
0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
我正在尝试确保至少一个字符落入从!
到~
的索引范围内,以确保密码中包含特殊字符。有人可以帮我吗?
这是我的代码:
import random
import string
def password_generator(length):
while True:
my_password = ''
for eachItem in range(length):
my_password += string.printable[random.randint(0, len(string.printable) - 7)]
if (any(x.isupper() for x in my_password) and any(x.islower() for x in my_password)
and any(x.isdigit() for x in my_password)):
return my_password
pass_length = int(input("Enter desired length of password: "))
print(password_generator(pass_length))
答案 0 :(得分:1)
更容易:
def minimumSteps(loggedMoves):
# Write your code here
moves = 0
for i in loggedMoves:
print(loggedMoves[i:])
if loggedMoves[i] == './' :
moves += 1
print(moves)
return moves
输出:
import random
import string
def password_generator(length):
allchar = string.printable # that is about what your choice of characters is
low = string.ascii_lowercase
hig = string.ascii_uppercase
spec = string.punctuation
nums = string.digits
pw = []
# ensure that each group has at least 1 character of these groups
pw.append(random.choice(low))
pw.append(random.choice(hig))
pw.append(random.choice(spec))
pw.append(random.choice(nums))
# and fill up with random from all groups
pw.extend(random.choices(allchar, k=length-len(pw)))
# mix the positions up
random.shuffle(pw)
# return as string
return "".join(pw)
pass_length = int(input("Enter desired length of password: "))
for _ in range(10):
print(password_generator(pass_length))
答案 1 :(得分:0)
您的主要建议是生成包含特殊字符的密码,而不是检查这些密码是否包含特殊字符,但是无论如何,如果要检查它,只需使用以下代码:
b]gDEK7:wM9_T__N:ugO
l1c2p3"rW)FB@=k]'1p~
9U=M'R3"Kbzqo/~8+Dr{
g`_w7tvL#Ulto&Q4Qi]"
O7(DttWffx4N7lr~B)h$
Azd2[HHTn:X!L\5^'\~`
%Sq}be2V<\eM^$$;)V@\
1}W{iBhV;u<D2@f5\m8P
E3vmhWxaWR'9hMeiU+1$