首页 > 解决方案 > 如何使用列表从用户制作密码生成器?

问题描述

我正在尝试在 python 中创建一个随机密码生成器。

如何使用用户从列表中选择 3 或 4 或 5 个随机数。

例如 :

numbers = ['0','1','2','3,','4','5','6','7','8',9']

numbers_in = int(input('how many numbers would you love to be in your password:').

如果我选择 4 作为用户,如何从 4791 等列表中获取四个随机数

标签: pythonlistloops

解决方案


干得好:

>>> from random import choice
>>> from string import digits
>>> N = int(input('How many numbers would you love to be in your password: '))
How many numbers would you love to be in your password: 4
>>> "".join(choice(digits) for _ in range(N))
'1786'

@furas 在评论中提供的最后一条语句的较短版本:

"".join(choices(digits, k=N))

推荐阅读