zoukankan      html  css  js  c++  java
  • 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重要了!!!
  • 相关阅读:
    spring MVC的启动过程详解
    BeanFactory和applicationContext之间的区别
    spring的事务管理
    通用Mybatis的Crud搭建
    spring的IOC原理
    spring的AOP原理
    TortoiseSVN使用简介
    SVN简明教程
    POJO
    velocity 框架
  • 原文地址:https://www.cnblogs.com/marcoxu/p/7158455.html
Copyright © 2011-2022 走看看