标签:三级菜单和模拟登录
模拟登陆:
1. 用户输入帐号密码进行登陆
2. 用户信息保存在文件内
3. 用户密码输入错误三次后锁定用户
自己写的代码
import getpass print("-------------welcome to our system----------------") flag=0 count=0 lock=0 while count < 3: username=input("username:") password=input("password:") Lockaccount = open(r"C:\Users\Administrator\PycharmProjects\untitled\day1\lockuser.txt", ‘r‘) for line1 in Lockaccount: if line1.strip() == username: print(username,"is locked") lock=1 break if lock == 1: break #for line1 in #password = getpass.getpass("password:") count +=1 File_Of_Useraccount = open(r"C:\Users\Administrator\PycharmProjects\untitled\day1\userinformation.txt") for line2 in File_Of_Useraccount: userinfo="{_username}:{_password}" .format(_username=username,_password=password) if line2.strip() == userinfo: print(username,"is logging....") print("welcome to this system") flag = 1; break if flag == 1: break File_Of_Useraccount.close() if count == 3: print(username,"is locked") File_Of_Lockacount = open(r"C:\Users\Administrator\PycharmProjects\untitled\day1\lockuser.txt",‘a‘) File_Of_Lockacount.write(username+‘\n‘) File_Of_Lockacount.close()
代码存在问题:
1:只要输入输错三次就自动锁定,并不是一个用户输入错误三次。
2:打开文件使用绝对路径,导致程序可移植性差。
改进后的代码:
import getpass print("-------------welcome to our system----------------") flag=0 count=0 lock=0 first_open_flag=0 while count < 3: if first_open_flag==0: username=input("username:") password=input("password:") first_open_flag=1 else: print("the password is wrong") password=input("please input %s password:" %(username)) Lockaccount = open("lockuser.txt", ‘r‘) for line1 in Lockaccount: if line1.strip() == username: print(username,"is locked") lock=1 break if lock == 1: break #for line1 in #password = getpass.getpass("password:") count +=1 File_Of_Useraccount = open("userinformation.txt","r") for line2 in File_Of_Useraccount: userinfo="{_username}:{_password}" .format(_username=username,_password=password) if line2.strip() == userinfo: print(username,"is logging....") print("welcome to this system") flag = 1; break #else: # print("the password is wrong") if flag == 1: break File_Of_Useraccount.close() if count == 3: print(username,"is locked") File_Of_Lockacount = open("lockuser.txt",‘a‘) File_Of_Lockacount.write(username+‘\n‘) File_Of_Lockacount.close()
100分答案
# Variables credentials = {} locked_info = [] authorized_account = [] user_attempts = [] secret = [] locked_indicator = False MAXIMUM_LOGIN = 3 no_of_attemps = 0 with open("secret.csv",‘r‘) as f: for line in f: real_username,real_passwd,locked = line.strip().split(‘,‘) credentials[real_username] = real_passwd if locked == ‘Y‘: locked_info.append(real_username) else: authorized_account.append(real_username) # #输入用户名密码 for login in range(MAXIMUM_LOGIN): user_input = input("username: ") if user_input in locked_info: print("Your account is LOCKED, please contact the administrator") break else: if user_input in authorized_account: user_attempts.append(user_input) password_input = getpass.getpass("passowrd: ") #认证成功后显示欢迎信息 if user_input in credentials.keys() and password_input in credentials.values(): print("Login successfully, welcome to my Homepage") break elif MAXIMUM_LOGIN - no_of_attemps > 1: no_of_attemps += 1 print("You have {} attempts left and the account will be locked".format(MAXIMUM_LOGIN - no_of_attemps)) elif MAXIMUM_LOGIN - no_of_attemps == 1: print("Your account is LOCKED, please contact the administrator to unlock it.") locked_indicator = True break else: print("We cannot recognize your username, pleaes input a correct one!") if locked_indicator: secret_list = (real_username,real_passwd,‘Y‘) secret.append(secret_list) with open("secret.csv",‘w‘) as f: writer = csv.writer(f) writer.writerow([‘username‘, ‘password‘, ‘locked‘]) writer.writerows(secret)
标签:三级菜单和模拟登录
原文地址:http://9475587.blog.51cto.com/9465587/1979749