【问题标题】:How to make a Password Generator from User using list? [duplicate]如何使用列表从用户制作密码生成器? [复制]
【发布时间】:2021-10-20 01:19:42
【问题描述】:

我正在尝试在 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 等列表中获取四个随机数

【问题讨论】:

标签: python list loops


【解决方案1】:

给你:

>>> 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 在 cmets 中提供的最后一条语句的较短版本:

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

【讨论】:

  • 您不需要列表。 join 接受一个迭代。 "".join(choice(digits) for _ in range(5))
  • @Sujay,谢谢,您的改进已添加 :)
  • 较短的choices(digits, k=N) - (在单词choices中带有字符s
  • @furas,很好!我不知道这个:)
猜你喜欢
  • 2019-05-10
  • 1970-01-01
  • 2022-10-06
  • 2020-12-28
  • 2019-10-23
  • 2022-09-23
  • 2012-07-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多