导入Python标准库中的random模块,用于生成随机数
import random
导入ascii_letters和digits变量,分别包含所有的大小写字母和数字。
from string import ascii_letters,digits
定义了一个名为randpass的函数,用于生成随机密码。
def randpass():
data=ascii_letters+digits
passwd=""
for i in range(10):
tmp = random.choice(data)
passwd+=tmp
print(passwd)
运行主程序
if __name__ == '__main__':
randpass()
完整代码
import random
from string import ascii_letters,digits
def randpass():
data=ascii_letters+digits
passwd=""
for i in range(10):
tmp = random.choice(data)
passwd+=tmp
print(passwd)
if __name__ == '__main__':
randpass()