Python学习之路:random模块

Posted Py小白

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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)

 

以上是关于Python学习之路:random模块的主要内容,如果未能解决你的问题,请参考以下文章