设置最小/最大长度随机。随机输出?

2024-04-25 22:56:12 发布

您现在位置:Python中文网/ 问答频道 /正文

所以为了使用我新学的python,我编写了一个随机密码的小工具。你知道吗

最终我得到了一个列表,比如说15个字符串。我要求用户输入他想要的密码数和密码长度。-相反,我想问他最小和最大长度是多少,而不是一个集合长度,并在我的结果中得到完整的范围。你知道吗

到目前为止,我的情况是:

items = ['5', '4', 'Y', 'T', 'R', 'E', 'h', 'g', 'f', 'd', '7', '1', '9', 'z', 'q', 'x']

x=int(raw_input("how many passwords would you like?:"))
z=int(raw_input("how long would you like the password to be?:"))

print("the list of possible passwords you received is:")
for i in range (x):
    random.shuffle(items)

输出结果如下:

TR1Eg47f9
5gRf9q7Ed
hxfE5gR4d
hE14x9Tz7

谢谢你。你知道吗


Tags: 工具the字符串you密码列表inputraw
1条回答
网友
1楼 · 发布于 2024-04-25 22:56:12

使用random.sample可以从列表中获得特定数量的项。你可以这样做:

from random import sample
print(''.join(sample(items, x)))

问题是x不能大于项目长度。您可以先进行一些修复,重复列表中的项目,如下所示:

while len(items) < x:
    items += items

相关问题 更多 >