Python学习之路:random模块

时间:2022-06-07 22:23:33

Python学习之路:random模块

Python学习之路:random模块

#随机生成4位数字的验证码
# import random
#
# checkcode=''
#
# for i in range(4):
# current=random.randint(1,9)
# checkcode+=str(current)
# print(checkcode)

#随机生成4位字母和数字组合的验证码
import random
checkcode=''
for i in range(4):
current=random.randrange(0,4)
if current==i:
tmp=chr(random.randint(65,90))
else:
tmp=random.randint(0,9)
checkcode+=str(tmp)
print(checkcode)