這是一個隨機生成四位字母和數字混合驗證碼, 並且進行輸入驗證的程序,和我們平時用到的驗證碼原理相同。
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 def check_code():#定義一個驗證碼生成程序 4 import random#引入random 5 checkcode=' ' 6 for i in range(4): 7 count = random.randrange(0,4) 8 if count!=i: 9 temp=chr(random.randint(65,90)) 10 else: 11 temp=random.randint(0,9) 12 checkcode+=str(temp) 13 return checkcode 14 while True:#生成一個循環驗證程序 15 code=check_code() 16 print(code) 17 a=code.lower() 18 b=a.lstrip()#對驗證碼左邊空白縮進 19 v=input("please input the code:") 20 if v.lower()!=b: 21 print("error") 22 else: 23 print("correct") 24 break