欢迎来到《Python 小项目》专栏,这个专栏会不定时更新Python的小项目,大家可以订阅关注哦!
这次,我们要编写一个密码生成器,先看效果:
密码长度:10
密码:pEIMX_JUbR
密码长度:8
密码:zl[sxebZ
密码长度:50
密码:_ja\szR{DLZxU@<yjM@[ldvtX?g>Mc?uSULuwS4sS8[9=ihsaq
代码教学:
for i in range(length):
password=password+random.choice(chars)
导入模块
import random
把字符添加到列表中,这次选择ASCII的第48到127项
chars=[chr(i) for i in range(48,127)]
询问密码长度
length=int(input("密码长度:"))
定义密码变量
password=""
添加字符进入密码的变量中
for i in range(length):
password=password+random.choice(chars)
输出密码
print(f"密码:{password}")
最终代码:
import random
chars=[chr(i) for i in range(48,127)]
length=int(input("密码长度:"))
password=""
for i in range(length):
password=password+random.choice(chars)
print(f"密码:{password}")
喜欢的话就点赞关注吧!
“Python 小项目”专栏是免费的,大家可以订阅哦!