1. 程式人生 > >python編寫登錄接口

python編寫登錄接口

word bmi file line 寫入 機會 lines ase ...

要求: 輸入用戶名密碼

   認證成功顯示歡迎信息

   輸錯三次以後鎖定

代碼如下:

# 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(‘輸入有錯誤,請重新輸入‘)




python編寫登錄接口