python学习-四位随机验证码

hcwei 2020年06月10日 428次浏览

代码如下

#!/usr/bin/python3
#Designed By HCWEI!
#Date:2018-12-07 17:00:00
#四位随机验证码小程序
import random
_ANS=""
for i in range(4):
    new=random.randint(0,4)
    if i == new:
       char=str(chr(random.randint(65,90)))
    else:
       char=str(random.randint(0,9))
    _ANS +=char
print("\033[1;30m验证码:\033[0m\033[1;7;31m %s \033[0m"% _ANS)
while not False:
  _INPUT = input("\033[1;33m请输入验证码:\033[0m")
  if _INPUT.lower() == _ANS.lower():
      print("\033[1;32m输入正确\033[0m")
      break
  else:
      print("\033[1;31m输入错误\033[0m")
      continue    

运行截图:
image.png