随机密码生成。编写程序,在26个字母大小写和10个数字随机生成10个8位密码。

  • 方法一:
import random
lis=[]
for i in range(ord('a'),ord('z')+1):
    lis.append(chr(i))
for i in range(ord('A'), ord('Z') + 1):
    lis.append(chr(i))
for i in range(0,10):
    lis.append(i)

for i in range(0,8):
    lis2 = random.sample(lis,8)
    for j in lis2:
        print(j,end="")
    print()

函数

描述

chr(x)

将一个整数转换为一个字符

ord(x)

将一个字符转换为它的整数值

  • 方法二:
import random
lis=["0","1","2","3","4","5","6","7","8","9",
     "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
     "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"
    ]
for i in range(0,10):
    lis2 = random.sample(lis,8)
    for j in lis2:
        print(j,end="")
    print()
  • 方法三:
import random
str="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
lis=list(str)#把字符串转换成列表
for i in range(0,10):
    lis2 = random.sample(lis,8)
    for j in lis2:
        print(j,end="")
    print()