python使用者登入系統

來源:互聯網
上載者:User
本文執行個體為大家分享了使用者登入系統python實現代碼,供大家參考,具體內容如下

注意事項:
1、使用python3執行程式。按提示輸入1或2,登入或退出程式
2、輸入使用者名稱後,如果使用者名稱被鎖定及無效使用者名稱,程式會退出,請確保輸入正確。
3、輸入使用者名稱正確後,輸入密碼。使用者名稱正確的情況下,密碼連續輸錯三次,使用者將被鎖定,禁止登入系統

#!/usr/local/env python#coding:utf-8#Auto:Panwenbin#function:lock_file=open('file_lock.txt','r+')user_pass=open('username_file.txt','r+')count=0

cmd=input(''' 1:登入系統 2:退出系統 請輸入您的操作:''') if cmd.isdigit() and int(cmd)==2: exit() elif cmd.isdigit() and int(cmd)==1:   while count <3:#只要重試不超過3次就不斷迴圈     username=input('請輸入您的使用者名稱:')     for i in lock_file.readlines(): #判斷使用者名稱是否在鎖檔案中       i=i.split()       if username in i[0]:         print('對不起 %s 鎖定'%username)         exit()

match = False     for j in user_pass.readlines():       user,password = j.strip('\n').split() #去掉每行多餘的\n並把這一行按空格分成兩列,分別賦值為user,passwd兩個變數       if username == user:   #判斷輸入的使用者是否存在           passwd=input ('請輸入密碼:')           if password == passwd:              print ('使用者名稱和密碼正確')              match = True              break          elif password != passwd:#在使用者名稱正確的前提下,判斷輸入的密碼是否正確              for i in range(2):               passwd=input ('密碼錯誤,請重新輸入密碼:')               if password == passwd:                  print ('使用者名稱和密碼正確')                  match = True                  break

if username != user :#判斷使用者不存在       print('您輸入使用者名稱不存,程式已退出')       exit()     elif match == False :#如果match還為False,代表上面的迴圈中跟本就沒有match上使用者名稱和密碼       print('密碼和使用者名稱不匹配,嘗試超過三次,使用者被鎖定')       lock_file.write('%s \n'%username)       lock_file.close()       user_pass.close()       exit()     elif match==True:       print('登入成功')       break else:   print('無效選項,程式已退出')

以上就是本文的全部內容,希望對大家學習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.