1. 程式人生 > >python編寫登入介面

python編寫登入介面

要求: 輸入使用者名稱密碼

      認證成功顯示歡迎資訊

   輸錯三次以後鎖定

程式碼如下:

# Author:YK
while(True):
select=input('請問是註冊還是登入')
if select == '註冊':
register='' #將需要儲存的user和password放在register中
username = input('please input your count...')
password = input('please input your password...')
register=register+username+','+password+'\n'
file1 = open('D:\\study\\code\\study_teach\\day1\\save_user.txt', 'a+',encoding='utf-8')
#開啟儲存使用者名稱和密碼的檔案
file1.write(register) #寫入檔案
file1.close()


elif select == '登入':
username = input('please input your count...')
file2 = open('D:\\study\\code\\study_teach\\day1\\lock.txt', 'r', encoding='utf-8')
#開啟被鎖定使用者的檔案
count=0
number=len(file2.readlines()) #獲取檔案行數
file2.seek(0, 0) #將滑鼠移動到首個位元組
while(count<number):
count=count+1
lines = file2.readline().strip('\n') #獲取每一行的使用者名稱字串
if lines == username: #判斷輸入的使用者名稱是否和儲存的使用者名稱相等
print('Your count {name} locked'.format(name=username))
file2.close()
exit()


file1 = open('D:\\study\\code\\study_teach\\day1\\save_user.txt', 'r',encoding='utf-8')
#開啟儲存使用者名稱和密碼的檔案
for i in range(3): #有3次機會輸入密碼的迴圈
password = input('please input your password...')
count = 0
number = len(file1.readlines()) #獲取檔案行數
file1.seek(0,0)
while (count < number):
count=count+1
line=file1.readline().strip('\n').split(',')
if line[0] == username and line[1] == password:
print('Welcom to you submit') #登入成功
exit() #退出
if i == 2: #3次均輸入錯誤
file2 = open('D:\\study\\code\\study_teach\\day1\\lock.txt', 'a+',encoding='utf-8')
file2.write(username+'\n') #將被鎖定的使用者名稱寫入被鎖檔案當中
file1.close()
file2.close()
print('Your count {name} locked'.format(name=username))
exit()
else:
print('輸入有錯誤,請重新輸入')