python - 如何使用列表从用户制作密码生成器?
问题描述
我正在尝试在 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 等列表中获取四个随机数
解决方案
干得好:
>>> 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))
推荐阅读
- iis - ASP.Net Web API 应用程序的 IIS HTTPERR Request_Cancelled 错误
- logging - 用于心电图的 Movesense 数据记录器
- docker - 在 docker 容器中具有 ssh 命令的 shell 脚本失败并显示 - ssh: not found
- firebase - Firebase BigQuery 导出广告系列效果
- r - R 是否使用应用降阶?
- python - 由于来自不同行的文本值在其他熊猫列中具有相同值的组合,因此创建新的熊猫行
- python - Python 脚本无法识别 Mysql 查询
- android - 无法在我在 android studio 中的改造回调中得到响应
- azure - 从 Azure DevOps 发布管道成功部署后,应用服务不加载 Web 应用
- svg - SVG - 具有百分比单位的中风破折号无法按预期工作