溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

python應(yīng)用文件讀取與登錄注冊(cè)功能

發(fā)布時(shí)間:2020-10-22 10:52:11 來(lái)源:腳本之家 閱讀:109 作者:紅桃五 欄目:開(kāi)發(fā)技術(shù)

python應(yīng)用文件讀取與登錄注冊(cè)功能,具體實(shí)現(xiàn)代碼如下所示:

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: zhw
#讀取文件中的內(nèi)容
def open_file(filename ,file_type , *args):
  #讀取文件中的數(shù)據(jù)
  if file_type == 'r':
    flie_list = []
    with open(filename,file_type) as sub_all:
      for file_line in sub_all:
        flie_list.append(file_line.strip())
      #print(flie_list)
      return flie_list
  #讀取文件中的用戶號(hào)
  elif file_type == 'ru':
    flie_list = []
    with open(filename,'r') as sub_all:
      for file_line in sub_all:
        file_l = file_line.split('|')
        user_no = file_l[0]
        flie_list.append(user_no.strip())
      #print(flie_list)
      return flie_list
  #寫(xiě)入文件
  elif file_type == 'a+':
    data = args[0]
    with open(filename, file_type) as sub_all:
      sub_all.write(data +"\n")
  else:
    print('您輸入的格式?jīng)]有,請(qǐng)檢查')
class User(object):
  def __init__(self, user_no, user_pw):
    self.user_no = user_no
    self.user_pw = user_pw
    self.file = 'login.log'
  @property
  #檢查賬號(hào)是否已經(jīng)存在
  def check_user(self):
    flie_list = open_file(self.file, 'ru')
    if self.user_no in flie_list:
      return '1'
    else:
      return '0'
  #注冊(cè)賬號(hào)
  def regist(self):
    user_info = '%s|%s' % (self.user_no, self.user_pw)
    open_file(self.file, 'a+',user_info)
    #return'注冊(cè)成功'
  #賬號(hào)登錄
  def login(self):
    flie_list = open_file(self.file, 'r')
    user_info = '%s|%s' % (self.user_no, self.user_pw)
    if user_info in flie_list:
      return '1'
    else:
      return '0'

def return_more(args):
  pass
#循環(huán)驗(yàn)證登錄,循環(huán)注冊(cè)賬號(hào)
while True:
  print('請(qǐng)選擇\n '
     '1. 注冊(cè)\n'
     '2. 登錄\n'
     '3. 退出')
  choose = input('請(qǐng)選擇').strip()
  if choose == '1':
    for i in range(3):
      user_no = input('請(qǐng)輸入注冊(cè)賬號(hào):').strip()
      user_pw = ''
      user = User(user_no,user_pw)
      #檢查用戶號(hào)是否存在
      return_more = user.check_user
      if return_more == '0':
        user_pw = input('請(qǐng)輸入注冊(cè)密碼:').strip()
        user = User(user_no, user_pw)
        return_more = user.regist()
        print(return_more)
        break
      else:
        print('您輸入的賬號(hào)已經(jīng)存在,請(qǐng)選擇其他賬號(hào)')
    #print('您輸入的錯(cuò)誤次數(shù)太多,已退出!')
  elif choose == '2':
     for i in range(3):
       user_no = input('請(qǐng)輸入登錄賬號(hào):').strip()
       user_pw = input('請(qǐng)輸入登錄密碼').strip()
       user = User(user_no, user_pw)
       #用戶登錄
       return_mk = user.login()
       if return_mk =='0':
         print("登錄失敗,請(qǐng)重新輸入")
       else:
         print('登錄成功')
         break
  elif choose == '3':
    break
  else:
    print('輸入序號(hào)有無(wú),請(qǐng)重新輸入')

總結(jié)

以上所述是小編給大家介紹的python應(yīng)用文件讀取與登錄注冊(cè)功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)億速云網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI