day5-random模块产生随机手机验证码

import random

# print(random.random()) #浮点数,0-1,无法指定range
# print(random.uniform(1,5)) #浮点数,1-5
# print(random.randint(1,100)) #1-99随机
# print(random.randrange(1,5)) #1-4随机
# print(random.choice('hello')) #取1位
# print(random.sample('hello',2)) #随机取2位

# x=[1,2,3,4,5,6]
# print(x)
# print(random.shuffle(x)) #随机序列
 1 checkcode=''    #刚开始为空
 2 
 3 for i in range(4):     #0-3, 轮询4次,相当于验证码长度4位
 4     bit=random.randrange(0,4)     #随机数0-3
 5     if bit==i:               #如正好在第1次出来的随机数也为1
 6         tmp=chr(random.randint(65,90))   #则将随机数转成ASCII码A-Z,65=A,90=Z
 7     else:
 8         tmp=random.randint(0,9)     #如正好在第1次出来的随机数不为1,则随机产生0-9的随机数
 9     checkcode+=str(tmp)     #if语句轮询4次,每次将tmp得到的值赋给checkcode
10 
11 print(checkcode)
12 #9ZM9

编程思路太TM重要了!!!

 

posted @ 2017-07-13 00:59  marcoxu  阅读(233)  评论(0编辑  收藏  举报