Python生成随机五位数——模仿手机验证码

使用Python生成随机的五位手机验证码。

# -*- coding:utf-8 -*-

#生成五位随机数,模仿手机验证码

#导入random库,可以生成随机数
import random

def ran():
     L = []
     M = []
#通过遍历5次,生成五个元素,并插入列表L
     for i in range(5):
             L.append(random.randint(0,9))
             if len(L) >= 5:
                     break

#通过遍历将L的五个元素由数字转为字符串,导入空列表M,并使用join方法合成为字符串     
     for d in L:
             M.append(str(d))
     S = '' .join(M)

     print(S)

#调用函数ran()
ran()

猜你喜欢

转载自blog.51cto.com/11433338/2389104