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)

 

posted @ 2017-12-25 13:48  Py小白  阅读(887)  评论(0编辑  收藏  举报