Python-S13-day1
需求:
1.让用户输入账号密码,账号密码正确,登陆程序,打印欢迎信息;
2.如果账号,或密码输入错误,提示用户重新输入,用户有三次机会;
3.如果用户第三次输入的账号或者密码还是不正确,就把第三次输入的账号锁定。
主程序:
#!/usr/bin/python env #_*_coding:utf-8 _*_ userfile1='userfile' lockfile1='lockfile' userfile=open(userfile1,'r+') lockfile=open(lockfile1,'r+') login_flag = False #设置一个标志位,用于判断用户是否登陆成功; for i in range(3): username=input("username:") for lockuser in lockfile.readlines(): lockuser = lockuser.split() #把被锁定的账号,转换成一个列表; if username in lockuser: #判断账号是否在锁定列表中,如果在,就退出; exit("%s user is lock"%username) password = input("password:") for line in userfile.readlines(): line = line.split() #把允许登陆的用户名,密码转换成列表; user = line[0] #把登陆账号赋值给user passwd = line[1] #把登陆密码赋值给passwd if user == username and passwd == password: #判断账号,密码是否正确,如果正确,登陆并打印欢迎信息; exit("welcomto login.") login_flag = True #标志位,判断用户是否登陆成功; if login_flag == False: print("user or passwd error.") else: print("%s user is lock."%username) lockfile.write("\t%s"%username) lockfile.close() #循环三次后,用户还没有登陆成功,就把账号锁定;
Readme:
1.这个程序一共三个文件:userfile存放登陆账号,密码的文件;
lockfile存放被锁定的账号;
login.py登陆主程序;
流程图:
思路二(功能全部实现):
#!/usr/bin/python env#_*_coding:utf-8 _*_user = open('db','r+')lock = open('db1','r+')mark = Falsefor i in range(3): username = input("input user name:") for line in lock.readlines(): line = line.split("|") if username in line: exit("user is lock.") passwd = input("input you password.") for pwd in user.readlines(): pwd = pwd.split("|") if pwd[0] == username and pwd[1].strip("\n") == passwd: exit("welcoto login.") else: print('user or passwd error.')else: print("%s user is lock"%username) lockuser = open('db1','a') lockuser.write("|"+"%s"%username) lockuser.close() * lockuser.txt 0||1|11
*loginuser.txt admin|123 freddy|123
思路三(函数写法):
#用户输入三次,密码不正确,锁定账号,功能没有实现; def register(user,pwd): reg = open('db','a') reg.write("\n" + user + "|" + pwd ) reg.close()def login (user,pwd): lockuser = open('db1','r') for line in lockuser.readlines(): line = line.split("|") if user in line: exit("user is lock.") userpwd = open('db','r') for logpwd in userpwd: logpwd = logpwd.split("|") if user == logpwd[0] and pwd == logpwd[1].strip("\n"): exit("welcome to login") else: print("user or password error") input_num = input("input y or n.") if input_num == 'y': main() else: exit()def main(): choice = input("1.register. 2.login.") if choice == '1': username = input("username:") password = input("password:") register(username,password) elif choice == '2': username = input("username:") password = input("password:") login(username,password)main()