http://www.iplaypy.com/python-100/
===============================================================
1.用户三次登陆机会,三次登录失败,验证码验证,验证成功继续三次登陆机会,验证失败退出。
代码:
1 def vertified(): 2 import random 3 temp = "" 4 for i in range(6): 5 num = random.randrange(0,4) 6 if num ==2 or num == 4: 7 rad_1 = random.randrange(0,10) 8 temp += str(rad_1) 9 else: 10 rad_2 = random.randrange(65,91) 11 temp += chr(rad_2) 12 print(temp) 13 return temp 14 15 16 17 def outer(func): 18 def inner(): 19 time = 1 20 while True: 21 name = input('please input your id:') 22 key = input('key:') 23 if name == 'vae' and key == '12345': 24 print("登陆成功!") 25 break 26 elif time == 3: 27 ert = vertified() 28 ret = input('check the verification code:' ) 29 if ret == ert: 30 continue 31 else: 32 print('登陆失败!') 33 break 34 else: 35 print('再次尝试!') 36 time += 1 37 continue 38 return inner 39 40 41 @outer 42 def lock(): 43 return 0 44 45 lock()
=============================================================================================
2.实现用户输入用户名和密码,程序在已有用户信息文件中检索,若一致则登陆成功!
1 def login(username,pwd): 2 s = open('s9.txt','r',encoding='utf-8') 3 for line in s: 4 generlize = line.strip() 5 line_list = generlize.split('$') 6 if username == line_list[0] and pwd == line_list[1]: 7 return True 8 return False 9 10 username = input('用户名:') 11 pwd = input('密码:') 12 13 is_login = login(username,pwd) 14 if is_login : 15 print('login sucessfully!') 16 else: 17 print('login failly')
========================================================================================================
3.用户登陆和注册(用户名占用or成功注册)
1 def login(username,pwd): 2 s = open('s9.txt','r',encoding='utf-8') 3 for line in s: 4 generlize = line.strip() 5 line_list = generlize.split('$') 6 if username == line_list[0] and pwd == line_list[1]: 7 return True 8 return False 9 10 def username_exist(username): 11 with open ('s9.txt','r',encoding = 'utf-8') as f: 12 for line in f: 13 generlization = line.strip() 14 line_list = generlization.split('$') 15 if line_list[0] == username: 16 return True 17 return False 18 19 def register(username,pwd): 20 with open('s9.txt','a',encoding = 'utf-8') as f: 21 temp = " "+ username + "$" + pwd 22 f.write(temp) 23 f.close() 24 25 def main(): 26 print('欢迎登录**系统'' ' 27 '1:登录;2:注册') 28 ret = input('您想要:') 29 if ret == "1": 30 username = input('用户名:') 31 pwd = input('密码:') 32 is_login = login(username, pwd) 33 if is_login: 34 print('login sucessfully!') 35 36 else: 37 print('login failly') 38 39 elif ret == "2": 40 username = input('用户名:') 41 pwd = input('密码:') 42 is_username = username_exist(username) 43 if is_username: 44 print('用户名被占用,注册失败') 45 else: 46 register(username, pwd) 47 print('注册成功!') 48 main()
==========================================================================================================