Python验证码简单实现(数字和大写字母组成的4位验证码)

#数字和英文大写字母的4位随机数
def checkcode(): #def 定义方法  checkcode() 方法名()
    import random # 导入包
    checkcode = ""
    string = range(0,4)
    for i in string:
        current = random.randrange(0,3) #randrange随机数  参数1<=随机数<=参数2
        if current != i:
            temp = chr(random.randint(65, 90))
        else:
            temp = random.randint(0,9)
        checkcode += str(temp)
    return checkcode #此方法返回值:checkcode(变量)
while True:
    yanzhengma = checkcode()
    print("验证码:",yanzhengma)
    user_in = str(input("请输入验证码:"))
    if user_in.lower() == yanzhengma.lower() :
        print("验证成功")
        break
    else :
        print("输入错误,请重新输入!")

运行结果

 

转载于:https://www.cnblogs.com/Dzc163/p/8484009.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值