作业内容:
(1)编写登录接口
(2)认证成功后显示欢迎信息
(3)输错三次锁定
以下为代码,分为两个部分的代码,一是register.py,另一个是login.py;其中register.py实现的注册功能,客户可输入用户名密码,用户名密码保存在txt文件中;login.py代码实现的是以下几个功能点:
(1)如果不存在该用户名,提示用户未注册;
(2)如果用户名在黑名单中,提示用户与管理员联系;
(3)如果密码输入正确,登录成功;如果密码输入错误,可以试三次,如果三次仍未成功,则将该用户名锁定,并记录到TXT中,下次再输入该用户名,则提示该用户已被锁定。
register.py
#!/usr/bin/env python #Author:Bing print("---------------") print("If you want to be a member of our web, please register.") print("---------------") username = input("please Enter your username: ") password = input("please Enter your password: ") filename1 = ‘username.txt‘ with open(filename1, ‘a‘) as file_object: file_object.write(username) file_object.write("\n") filename2 = ‘password.txt‘ with open(filename2, ‘a‘) as file_object: file_object.write(password) file_object.write("\n")
login.py
#!/usr/bin/env python #Author:Bing usernames = [] passwords = [] usrpwddict = {} count = 0 filename = ‘blacklist.txt‘ with open(‘username.txt‘) as file_object: for username_ in file_object.readlines(): username_ = username_.strip(‘\n‘) usernames.append(username_) with open(‘password.txt‘) as file_object: for password_ in file_object.readlines(): password_ = password_.strip(‘\n‘) passwords.append(password_) usrpwddict = dict(zip(usernames,passwords)) print(usernames) print("---------------") print("Login") print("---------------") usernamelist = [] usrpwd = [] username = input("Please enter your username: ") if username not in usernames: print("You have not register yet! please register first!") else: with open(‘blacklist.txt‘) as file_object: for usernames_ in file_object.readlines(): usernames_ = usernames_.strip(‘\n‘) usernamelist.append(usernames_) for i in usernamelist: if username == i: print("Your Username is locked, Please contact the administrator!!!") break else: while count < 3: password = input("Please Enter your password: ") if usrpwddict[username] == password: print("congratulation!") break else: count += 1 if count == 3: print("You have tried too much times, now you are locked!") with open(filename, ‘a‘) as file_object: file_object.write(username) file_object.write("\n") break print("try again!")