随机数模块:
import random #随机小数
print(random.random())
#随机整数
print(random.randint(1,5))#他会打印5
#随机整数
print(random.randrange(1,5))#他永远不会打印5
利用random和string模块实现随机验证码:
import string print(string.letters)
#abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
print(string.digits)
#
print(string.ascii_letters)
#abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ #方式一
str_source = string.ascii_letters+string.digits
print(''.join(random.sample(str_source,4))) #方式二 checkcode = ''
for i in range(6):
current = random.randrange(0,6)
if current != i :
temp = chr(random.randint(65, 90))
else:
temp = random.randint(0,9) checkcode += str(temp) print(checkcode)