python - 密码生成器的字符顺序如何随机化

标签 python python-3.x random

目前代码仅按顺序随机化,首先是字母,然后是数字,最后是符号,我希望它以任何顺序随机化,没有顺序,如何实现?

#Password Generator Project
import string
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"))
l_password = ""
s_password = ""
n_password = ""


# Eazy Level - Order not randomised:5

# e.g. 4 letter, 2 symbol, 2 number = JduE&!91

for letter in range(nr_letters):
  l_password += random.choice(letters)


for symbol in range(nr_symbols):
  s_password += random.choice(symbols)


for number in range(nr_numbers):
  n_password += random.choice(numbers)


final_pass = str(l_password) + str(s_password) + str(n_password)
print(f"Here is your password :{final_pass}")


#Hard Level - Order of characters randomised:
#e.g. 4 letter, 2 symbol, 2 number = g^2jk8&P


最佳答案

您可以使用random.shuffle来随机排列结果:

final_pass = str(l_password) + str(s_password) + str(n_password)
l = list(final_pass)
random.shuffle(l)
final_pass = ''.join(l)

关于python - 密码生成器的字符顺序如何随机化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65642180/

相关文章:

python - 如何在 Python 中使用 Kerberos 库?

python - 如何不把自己。初始化 Python 类中的每个属性时?

python - 在 pandas/python 中创建多个子数据框

python - 按下 tkinter 按钮即可调用函数

python-3.x - JAX 与 NumPy 数组索引 - 越界行为

python-3.x - 执行 select pg_notify 的 Python psycopg2 不起作用

perl - 在perl中随机化矩阵,保持行和列的总数相同

python - Matplotlib 毫秒在 x 轴上打勾

random - 为什么在lua的某些平台上第一个随机数总是相同的?

java - 按排序顺序返回唯一条目的随机数生成器