第一模塊第一次作業

模擬登陸:

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)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章