zoukankan      html  css  js  c++  java
  • 6位数随机验证码

    通过模块random与内置方法chr进行配合

    方法‘chr’:

    把数字0-255转换成ASSIC码对应值

    使用方法:

    >>> for i in range(10):
    ... chr(i)
    ...
    'x00'
    'x01'
    'x02'
    'x03'
    'x04'
    'x05'
    'x06'
    'x07'
    'x08'
    ' '

    random 模块:

     该模块中的常见方法——

    random.random()  #不需要参数,直接生成随机浮点数

    >>> random.random()
    0.34820468445629016
    >>> random.random()
    0.2020452902294988
    >>> random.random()
    0.7249132577745135

    random.randint() #在给定的int范围内生成随机数

    >>> random.randint(1,4)
    4
    >>> random.randint(1,4)
    1
    >>> random.randint(1,4)
    1
    >>> random.randint(1,4)
    3
    >>> random.randint(1,4)
    1
    >>> random.randint(1,4)
    1
    >>> random.randint(1,4)

    random.randrange() #给定一个数字a,从0-a提取随机数

    >>> random.randrange(3)
    0
    >>> random.randrange(3)
    1
    >>> random.randrange(3)
    2

    制作一个小程序,生成6位数随机验证码: #简单验证码,通用脚本#

    import random
    
    check_code = " "
    
    for i in range(6):
        value = random.randint(0,6)
        if value != i :
            result = str(chr(random.randint(1,95)))
        else:
            result = random.randint(0,9)
        check_code += str(result)
    print(check_code)
  • 相关阅读:
    产品设计理应遵循哪些原则?
    产品经理必读的九步法
    exec
    Class convert
    Connecting method
    ASP.NET读写操作
    Encrypt Decrypt
    EventHandler, EventArgs
    Convert using code
    Dictionary List
  • 原文地址:https://www.cnblogs.com/alben-cisco/p/6917183.html
Copyright © 2011-2022 走看看