python用户登录系统
本文实例为大家分享了用户登录系统python实现代码,供大家参考,具体内容如下
注意事项:
1、使用python3执行程序。按提示输入1或2,登录或退出程序
2、输入用户名后,如果用户名被锁定及无效用户名,程序会退出,请确保输入正确。
3、输入用户名正确后,输入密码。用户名正确的情况下,密码连续输错三次,用户将被锁定,禁止登录系统
#!/usr/local/env python#coding:utf-8#Auto:Panwenbin#function:lock_file=open('file_lock.txt','r+')user_pass=open('username_file.txt','r+')count=0
立即学习“Python免费学习笔记(深入)”;
cmd=input(''' 1:登录系统 2:退出系统 请输入您的操作:''') if cmd.isdigit() and int(cmd)==2: exit() elif cmd.isdigit() and int(cmd)==1: while count <p></p><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/00968c3c2c15" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">Python免费学习笔记(深入)</a>”;</p><p class="jb51code"></p><pre class="brush:py;">match = False for j in user_pass.readlines(): user,password = j.strip('').split() #去掉每行多余的并把这一行按空格分成两列,分别赋值为user,passwd两个变量 if username == user: #判断输入的用户是否存在 passwd=input ('请输入密码:') if password == passwd: print ('用户名和密码正确') match = True break elif password != passwd:#在用户名正确的前提下,判断输入的密码是否正确 for i in range(2): passwd=input ('密码错误,请重新输入密码:') if password == passwd: print ('用户名和密码正确') match = True break
立即学习“Python免费学习笔记(深入)”;
if username != user :#判断用户不存在 print('您输入用户名不存,程序已退出') exit() elif match == False :#如果match还为False,代表上面的循环中跟本就没有match上用户名和密码 print('密码和用户名不匹配,尝试超过三次,用户被锁定') lock_file.write('%s '%username) lock_file.close() user_pass.close() exit() elif match==True: print('登录成功') break else: print('无效选项,程序已退出')
立即学习“Python免费学习笔记(深入)”;