鱼C论坛

 找回密码
 立即注册
查看: 1337|回复: 3

[已解决]随机产生密码

[复制链接]
发表于 2022-4-4 23:51:04 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
import random
k=random.randint(8,12)
ls=[0 for i in range (0,k)]
ls1=[random.randint(0,k)for i in range(0,random.randint(1,k-1))]
for i in ls1:
    ls[i]=chr(random.randint(97,122))
ls2=[random.randint(0,k)for i in range(0,random.randint(1,k-1-len(ls1)))]
for i in range(len(ls2)):
    if ls2[i] not in ls1:
        ls[ls2[i]]=chr(random.randint(65,90))
    else:
        False
ls3=[random.randint(0,k)for i in range(0,random.randint(1,k-1-len(ls1)-len(ls2)))]
for i in range(len(ls3)):
    if ls3[i] not in ls1+ls2:
        ls[ls3[i]]=random.choice('$','#','%','&')
    else:
        False
ls4=[random.randint(0,k)for i in range(0,random.randint(1,k-1-len(ls1)-len(ls2)-len(ls3)))]
for i in range(len(ls4)):
    if ls4[i] not in ls1+ls2+ls3:
        ls[ls4[i]]=random.randint(0,9)
    else:
        False
print (ls)

ls[i]=chr(random.randint(97,122))
IndexError: list assignment index out of range
第六行这里为什么说超出范围了啊?

ls[ls3[i]]=random.choice('$','#','%','&')
TypeError: choice() takes 2 positional arguments but 5 were given
请问这样报错是什么意思啊
最佳答案
2022-4-5 00:05:11
wadmdmaw 发表于 2022-4-4 23:51
题目:随机产生一个8到12位密码,要求包含英文字符的大小写、数字字符以及至少一个$,#,%,&中的特殊字符

random.choice(['$','#','%','&'])
或者
random.choice('$#%&')
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-4-4 23:51:22 | 显示全部楼层
题目:随机产生一个8到12位密码,要求包含英文字符的大小写、数字字符以及至少一个$,#,%,&中的特殊字符
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-4-5 00:05:11 From FishC Mobile | 显示全部楼层    本楼为最佳答案   
wadmdmaw 发表于 2022-4-4 23:51
题目:随机产生一个8到12位密码,要求包含英文字符的大小写、数字字符以及至少一个$,#,%,&中的特殊字符

random.choice(['$','#','%','&'])
或者
random.choice('$#%&')
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-4-5 17:25:37 | 显示全部楼层
好的,谢谢!可以再请问一下第六行这里为什么总是显示超出范围了啊?list assignment is out of range?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-29 06:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表