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()

原文地址:https://blog.51cto.com/11433338/2389104

时间: 2024-08-27 02:27:03

Python生成随机五位数——模仿手机验证码的相关文章

Python 生成随机验证码

Python生成随机验证码 Python生成随机验证码,需要使用PIL模块. 安装: 1 pip3 install pillow 基本使用 1. 创建图片 1 2 3 4 5 6 7 8 9 from PIL import Image img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255)) # 在图片查看器中打开 # img.show()  # 保存在本地 with open('code.png','wb') as f

python生成随机验证码

Python 生成随机验证码,需安装 PIL模块 安装: pip3 install pillow 基本使用 1,.创建图片 from PIL import Image img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255)) # 在图片查看器中打开 # img.show() # 保存在本地 with open('code.png','wb') as f: img.save(f,format='png') 2.创建画笔,用

Python生成随机字符串

利用Python生成随机域名等随机字符串. #!/usr/bin/env python# -*- coding: utf-8 -*- from random import randrange, choice from string import ascii_lowercase as lc from sys import maxsize from time import ctime tlds = ('com', 'edu', 'net', 'org', 'gov') for i in range(

python生成随机日期字符串

原文来自:  python生成随机日期字符串 生成随机的日期字符串,用于插入数据库. 通过时间元组设定一个时间段,开始和结尾时间转换成时间戳. 时间戳中随机取一个,再生成时间元组,再把时间元组格式化输出为字符串 import time import random a1=(1976,1,1,0,0,0,0,0,0) #设置开始日期时间元组(1976-01-01 00:00:00) a2=(1990,12,31,23,59,59,0,0,0) #设置结束日期时间元组(1990-12-31 23:59

python 生成随机字符串 和随机数字

生成随机字符串: ''.join(random.sample(string.ascii_letters + string.digits, 8)) //字符串中不会重复 for _ in range(8): s += random.choice(string.ascii_letters + string.digits) //字符串可以重复 生成随机浮点数 random.uniform(10, 20) 生成随机整数 random.randint(12, 20) 原文地址:https://www.cn

使用Python生成随机简单的验证码

简单的生成验证码 import random code = [] for i in range(5): if i == random.randint(1,9):#随机生成1-9的数字 code.append(str(random.randint(1,9))) else: temp = random.randint(65,90) code.append(chr(temp))#随机生成A-Z的字母 print ''.join(code)

python 生成随机优惠码(随机字符串)

第 0001 题:做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)? https://github.com/Yixiaohan/show-me-the-code import random codeLength = 12 codeCount = 200 codeOrig = "ABCDEFGHIJKLMNOPQRST1234567890" def makePromoteCo

自省 另外一种python 生成随机在base36 之间的兑换码生成。

放假无聊,翻看自己博客的时候发现自己前面写的 那个base36兑换码在翻阅的时候 想到一个更简单的办法实现.但是随机上来说可能没有前者那么高 但是觉得也没有多大的问题 发上来 自己再想想 import string import random maka = string.digits + string.ascii_letters maka_list = list(maka) x = [random.choice(maka_list) for i in range(6)] print ''.joi

Python 脚本生成测试数据,Python生成随机数据,Python生成大量数据保存到文件夹中

代码如下: import random import datetime import time dataCount = 10*100*100 #10M. codeRange = range(ord('a'),ord('z')) alphaRange = [chr(x) for x in codeRange] alphaMax = len(alphaRange) daysMax = 42003 theDay = datetime.date(1900,1,1) def genRandomName(n