#数字和英文大写字母的4位随机数 def checkcode(): #def 定义方法 checkcode() 方法名()import random # 导入包checkcode = ""string = range(0,4)for i in string:current = random.randrange(0,3) #randrange随机数 参数1<=随机数<=参数2if 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("验证成功")breakelse :print("输入错误,请重新输入!")
运行结果