鱼C论坛

 找回密码
 立即注册
查看: 2206|回复: 19

[已解决]随机产生一个8到12位密码

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

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

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

x
随机产生一个8到12位密码,要求包含英文字符的大小写、数字字符以及至少一个$,#,%,&中的特殊字符
import random
k=random.randint(8,12)
ls=[0 for i in range (0,k)]
ls1=[j in range(0,k)for i in range(0,random.randint(1,k-1))]
for i in range(len(ls1)):
    ls[ls1[i]]=chr(random.randint(97,122)
ls2=[a in range(0,k)for i in range(0,random.randint(1,k-1-len(ls1)))]
for i in range(len(ls2)):#请问一下这里结尾的英文冒号和开头的ls的l为什么总是报错“invalid synax”?
    if a not in ls1:
        ls[ls2[i]]=upper(chr(random.randint(97,122)))
    else:
        False
ls3=[b in range(0,k)for i in range(0,random.randint(1,k-1-len(ls1)-len(ls2)]
for i in range(len(ls3)):
    if b not in ls1+ls2:
        ls[ls3[i]]=random.choice('$','#','%','&')
    else:
        False
ls4=[c in range(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 c not in ls1+ls2+ls3:
        ls[ls4[i]]=random.randint(0,9)
    else:
        False
print (ls)

请问这样写可以吗?为什么运行出来总是在英文冒号和ls的l那里报错?
最佳答案
2022-4-4 18:30:50
wadmdmaw 发表于 2022-4-4 18:18
但是位置也要随机,完全随机,不一定小写字母之后是大写字母然后是特殊字符...

  1. from random import randint, choice, shuffle


  2. alphabet = "abcdefghijklmnopqrstuvwxyz"

  3. length = randint(8, 12)

  4. password1 = [choice(alphabet) for _ in range(length // 4)]
  5. password2 = [choice(alphabet.upper()) for _ in range(length // 4)]
  6. password3 = [choice("$#%&") for _ in range(length // 4)]
  7. password4 = [str(randint(0, 9)) for _ in range(length - length // 4 * 3)]

  8. password = password1 + password2 + password3 + password4
  9. shuffle(password)
  10. password = "".join(password)
  11. print(password)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-4-4 16:21:20 From FishC Mobile | 显示全部楼层
错误信息发一下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-4-4 16:45:46 | 显示全部楼层
因为你有很多括号忘记了。

我不会修改你的代码,我就自己做一个吧。
  1. from random import randint, choice


  2. alphabet = "abcdefghijklmnopqrstuvwxyz"

  3. length = randint(8, 12)

  4. password1 = [choice(alphabet) for _ in range(length // 3)]
  5. password2 = [choice(alphabet.upper()) for _ in range(length // 3)]
  6. password3 = [choice("$#%&") for _ in range(length - length // 3 * 2)]

  7. password = "".join(password1 + password2 + password3)
  8. print(password)
复制代码


有问题继续找我哦。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-4-4 16:46:47 From FishC Mobile | 显示全部楼层
  1. """
  2. 随机产生一个8到12位密码,要求包含英文字符的大小写、数字字符以及至少一个$,#,%,&中的特殊字符
  3. """
  4. import random

  5. A = [chr(i) for i in range(ord('A'), ord('Z') + 1)]
  6. B = [chr(i) for i in range(ord('a'), ord('z') + 1)]
  7. C = ['$', '#', '%', '&']

  8. def check(password):
  9.         if not password:
  10.                 return False
  11.         a = b = c = False
  12.         for each in password:
  13.                 if each in A: a = True
  14.                 elif each in B: b = True
  15.                 elif each in C: c = True
  16.         return all([a, b, c])

  17. if __name__ == "__main__":
  18.         password = None
  19.         while not check(password):
  20.                 password = random.sample(A + B + C, random.randint(8, 12))
  21.         print(''.join(password))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-4-4 18:14:42 | 显示全部楼层
wp231957 发表于 2022-4-4 16:21
错误信息发一下

就是这里:for i in range(len(ls2)):#请问一下这里结尾的英文冒号和开头的ls的l为什么总是报错“invalid synax”?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-4-4 18:18:26 | 显示全部楼层
ckblt 发表于 2022-4-4 16:45
因为你有很多括号忘记了。

我不会修改你的代码,我就自己做一个吧。

但是位置也要随机,完全随机,不一定小写字母之后是大写字母然后是特殊字符...
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-4-4 18:19:14 | 显示全部楼层
ckblt 发表于 2022-4-4 16:45
因为你有很多括号忘记了。

我不会修改你的代码,我就自己做一个吧。

还要包括数字字符,可以请问一下我具体是哪里括号有问题么,可以麻烦指出一下么
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-4-4 18:24:53 | 显示全部楼层

运行了一下好像没有数字字符呢?还有可以请问一下我写的出现报错invalid synax是怎么回事么...
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-4-4 18:30:50 | 显示全部楼层    本楼为最佳答案   
wadmdmaw 发表于 2022-4-4 18:18
但是位置也要随机,完全随机,不一定小写字母之后是大写字母然后是特殊字符...

  1. from random import randint, choice, shuffle


  2. alphabet = "abcdefghijklmnopqrstuvwxyz"

  3. length = randint(8, 12)

  4. password1 = [choice(alphabet) for _ in range(length // 4)]
  5. password2 = [choice(alphabet.upper()) for _ in range(length // 4)]
  6. password3 = [choice("$#%&") for _ in range(length // 4)]
  7. password4 = [str(randint(0, 9)) for _ in range(length - length // 4 * 3)]

  8. password = password1 + password2 + password3 + password4
  9. shuffle(password)
  10. password = "".join(password)
  11. print(password)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-4-4 18:36:54 | 显示全部楼层
  1.     ls[ls1[i]]=chr(random.randint(97,122)
  2.     ls[ls1[i]]=chr(random.randint(97,122))
复制代码
  1. ls3=[b in range(0,k)for i in range(0,random.randint(1,k-1-len(ls1)-len(ls2)]
  2. ls3=[b in range(0,k)for i in range(0,random.randint(1,k-1-len(ls1)-len(ls2)))]
复制代码
  1. ls4=[c in range(0,k)for i in range(0,random.randint(1,k-1-len(ls1)-len(ls2)-len(ls3)]
  2. ls4=[c in range(0,k)for i in range(0,random.randint(1,k-1-len(ls1)-len(ls2)-len(ls3)))]
复制代码


改完以后还有一大堆未定义:
Snipaste_2022-04-04_18-35-06.png

基本都是 for i in ... ,结果调用的时候是 a,b
还有一个 upper('...') 应该是 '...'.upper()
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-4-4 18:46:59 | 显示全部楼层
ckblt 发表于 2022-4-4 18:36
改完以后还有一大堆未定义:

ls2=[a in range(0,k)for i in range(0,random.randint(1,k-1-len(ls1)))]
请问一下我定义列表的时候这里用的a之后我用a表示列表中的元素就是invalid synax了么?
for i in range 循环里的变量只能有i么
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-4-4 18:52:21 | 显示全部楼层
ckblt 发表于 2022-4-4 18:36
改完以后还有一大堆未定义:

import random
k=random.randint(8,12)
ls=[0 for i in range (0,k)]
ls1=[j in range(0,k)for i in range(0,random.randint(1,k-1))]
for i in range(len(ls1)):
    ls[ls1[i]]=chr(random.randint(97,122)
ls2=[a in range(0,k)for i in range(0,random.randint(1,k-1-len(ls1)))]
不知道怎么发图片...就是运行之后第一次报错是在这里的最后一行开头的这个l的地方,不知道是哪里有问题...这样不就是在定义列表么?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-4-4 19:04:48 | 显示全部楼层
wadmdmaw 发表于 2022-4-4 18:24
运行了一下好像没有数字字符呢?还有可以请问一下我写的出现报错invalid synax是怎么回事么...

invalid synax 表示语法错误。

代码已增加数字:
  1. """
  2. 随机产生一个8到12位密码,要求包含英文字符的大小写、数字字符以及至少一个$,#,%,&中的特殊字符
  3. """
  4. import random

  5. A = [chr(i) for i in range(ord('A'), ord('Z') + 1)]
  6. B = [chr(i) for i in range(ord('a'), ord('z') + 1)]
  7. C = ['$', '#', '%', '&']
  8. D = [str(i) for i in range(10)]

  9. def check(password):
  10.         if not password:
  11.                 return False
  12.         a = b = c = d = False
  13.         for each in password:
  14.                 if each in A: a = True
  15.                 elif each in B: b = True
  16.                 elif each in C: c = True
  17.                 elif each in D: d = True
  18.         return all([a, b, c, d])

  19. if __name__ == "__main__":
  20.     password = None
  21.     while not check(password):
  22.         password = random.sample(A + B + C + D, random.randint(8, 12))
  23.     print(''.join(password))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2022-4-5 11:29:39 | 显示全部楼层
wadmdmaw 发表于 2022-4-4 18:46
ls2=[a in range(0,k)for i in range(0,random.randint(1,k-1-len(ls1)))]
请问一下我定义列表的时候这 ...

你非要用 a 的话,你用 for a in ... 也行
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-4-5 17:51:20 | 显示全部楼层
傻眼貓咪 发表于 2022-4-4 19:04
invalid synax 表示语法错误。

代码已增加数字:

谢谢大佬!目前我的水平看这个感觉有点太复杂了…还是想在现在已经写出了来的的基础上改进一下…
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-4-5 17:53:39 | 显示全部楼层
ckblt 发表于 2022-4-5 11:29
你非要用 a 的话,你用 for a in ... 也行

好的,
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))
改进之后最后一行这里ls[i]运行为什么总会报错超出范围了呢?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-4-5 21:52:13 | 显示全部楼层
  1. import random as r
  2. a = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890`!@#$%^&*()_+-=\[]{},./<>?;:/*-+"\''
  3. long = len(a)
  4. t = ''
  5. b = int(input("长度:"))
  6. for e in range(10):
  7.     t = t+a[r.randint(0,long-1)]
  8. print(t)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-4-8 14:05:28 | 显示全部楼层
b是赋值了之后又没用了么?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-4-8 14:07:12 | 显示全部楼层

还有这样的话不能保证生成的一定包含大小写字母数字和特殊字符啊?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-4-8 15:21:14 | 显示全部楼层
  1. import random
  2. import itertools
  3. import string

  4. total_num = random.randint(8, 12)
  5. split_pos = random.choice(list(itertools.combinations(list(range(1, total_num)), 3)))
  6. a_num, A_num, dig_num, sp_num = [i - j for i, j in zip(split_pos + (total_num,), (0,) + split_pos)]

  7. lis = ([random.choice(string.ascii_lowercase) for _ in range(a_num)]
  8.        + [random.choice(string.ascii_uppercase) for _ in range(A_num)]
  9.        + [random.choice(string.digits) for _ in range(dig_num)]
  10.        + [random.choice('$#%&') for _ in range(sp_num)])
  11. random.shuffle(lis)
  12. password = ''.join(lis)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-29 09:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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