Python基礎練習之使用者登入實現代碼分享,python使用者登入

來源:互聯網
上載者:User

Python基礎練習之使用者登入實現代碼分享,python使用者登入

python版本為python3.5

1.要求

1)輸入使用者名稱密碼
2)認證成功後顯示歡迎資訊
3)輸錯三次後鎖定

2.需求分析

1)使用者資訊儲存在檔案中(login/config/user_login.txt)
2)使用者輸入使用者名稱和密碼
3)判斷使用者名稱是否存在,存在則繼續,不存在則提示繼續輸入
4)判斷輸入的使用者名稱是否已經被鎖定,如果鎖定則退出程式,否則繼續
5)匹配檔案中的使用者資訊
6)如果匹配則列印出歡迎資訊
7)如果輸入3次密碼錯誤,則鎖定該使用者名稱(login/config/name_lock.txt)

3.測試使用者

bigberg:123abc
lc:123456
smallberg:111111
root:12345
dinasor:12321

# -*- coding: UTF-8 -*-#Author:Bigberg#定義一個迴圈計數count = 0#定義一個字典儲存使用者名稱和密碼names={}#定義一個列表格儲存體使用者名稱name_exit = []#輸入使用者名稱username = input("username:")#判斷輸入的使用者的使用者是否在使用者名稱單中with open("../config/name_login.txt",'r') as f:  while True:    line = f.readline().strip()    if not line:      break    else:      name = line.split(':')[0]      passwd = line.split(':')[1]      names[name] = passwdfor key in names:  name_exit.append(key)#判斷使用者名稱是否正確,不正確則繼續輸入while username not in name_exit:  print("The account is not exit.Check it again.")  username = input("username:")else:  # 讀取鎖定檔案中的內容  with open("../config/name_lock.txt", "r") as f:    lock_name = f.read()  # 判斷使用者名稱是否在鎖定檔案中,如果在就退出程式  if username == lock_name:    print("Sorry.Your account has been locked.")    exit()  else:    # 3次輸入密碼的機會    while count < 3:      passwd_input = input("password:")      # 判斷使用者名稱和密碼是否有效      if passwd_input == names[username]:        print("Welcome!", username)        break      else:        print("Error,please try again.")      count += 1      # 如果3次輸入密碼錯誤,則將使用者名稱添加到鎖定檔案      if count == 3:        with open("../config/name_lock.txt", "w") as f:          f.write("%s" % username)        print("You have tried 3 times,and your account will be locked")

總結

以上就是本文關於Python基礎練習之使用者登入實現代碼分享的全部內容,希望對大家有所協助。感興趣的朋友可以繼續參閱本站:Python入門之三角函數全解【收藏】、python好玩的項目—色情圖片識別代碼分享、Python實現一個簡單的驗證碼程式等,有什麼問題可以隨時留言,小編會及時回複大家的。感謝朋友們對本站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.