基于Python的用戶管理小系統,包含文件讀寫操作,實現了用戶信息注冊和登錄的簡單功能。
class userLogReg:  """  Created on 2018.11  @author: tox33  """  def __init__(self):    """      :param userFile: 操作的文件    """    self.userFile = "user.txt"   def userLogin(self,username,password):    """      用戶登錄      :param username:用戶名      :param paaword:密碼      :return:True,用戶登錄成功;False,用戶登錄失敗    """    try:      f = open(self.userFile,"r",encoding="utf-8")      for line in f:        line = line.strip()        line_list = line.split("#")        if line_list[0] == username and line_list[1] == password:          return True        elif line_list[0] == username and line_list[1] != password:          print("密碼錯誤!!")      return False    except IOError:      return False   def userRegister(self,username,password):    """      用戶注冊      1、打開文件      2、用戶名#密碼      :param username:用戶名      :param password:密碼      :return:True,注冊成功    """    with open(self.userFile,"a",encoding="utf-8")as f:      temp = "/n" + username + "#" + password      f.write(temp)      return True   def user_exist(self,username):    """      檢測用戶名是否存在      :param username:要檢測的用戶名      :return: True,用戶名存在;False,用戶名不存在    """    try:      with open(self.userFile,"r",encoding="utf-8") as f:        for line in f:          line = line.strip()          line_new = line.split("#")          if line_new[0] == username:            return True        return False    except IOError:      return False   def main(self):    """      主控制函數      :操作選擇參數arg: 0-注冊 1-登錄    """    print("歡迎來到Al用戶管理系統")    while(True):      arg = input("0:注冊 ,1:登錄/n")      if arg == "0":        user = input("請設置用戶名:")        if self.user_exist(user):          print("用戶名已存在,請重新設置!")          continue        else:          pwd = input("請設置密碼:")          if self.userRegister(user,pwd):            print("注冊成功!")            continue          else:            print("注冊失敗!")            continue      elif arg == "1":        user = input("請輸入用戶名:")        if not self.user_exist(user):          print("用戶名不存在,請檢查!")          continue        else:          pwd = input("請輸入登錄密碼:")          if self.userLogin(user,pwd):            print("登錄成功!")            break          else:            print("登錄失敗,請檢查!")            continue      else:        print("輸入錯誤,請檢查!")        continue if __name__ == '__main__':  test = userLogReg()  test.main()參考網址:Python登錄注冊驗證功能實現
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林站長站。
新聞熱點
疑難解答