标签:password welcome correct 用户名 python
编写登录接口
--输入用户名和密码
--认证成功后显示欢迎信息
--输错三次后锁定
version-1
-------------------------------
account_file = ‘account.txt‘
lock_file = ‘lock.txt‘
for i in range(3):
username = raw_input("username: ").strip()
password = raw_input("password:").strip()
if len(username) !=0 and len(password) !=0:
f = file(‘account_file‘)
loginSuccess = False
for line in f.readlines():
if username == line.split()[0] and password == line.split()[1]: #user and passwd are correct
print "Welcome %s login my system" % username
break
if loginSuccess is True: #login success
break
else:
continue
-------------------------------
version-2
-------------------------------
import os
account_file = ‘account.txt‘
lock_file = ‘loack.txt‘
# put account in a list
f =file(account_file)
account_list = f.readlines()
f.close()
while True:
# put locked user into a lock list
f = file(lock_file)
lock_list = []
for i in f.readlines():
line=i.strip(‘\n‘)
lock_list.append(line)
f.close()
loginSuccess = False
userame =raw_input(‘username: ‘).strip()
if username in lock_list:
print "Sorry,you are already in the block list,getthe fuking out!"
break
for line in account_list:
line = line.split()
if line[0] == username: #username correct
for i in range(3):
password = raw_input(‘passwd:‘).strip()
if password == line[1]: #paswd correct
print "Welcome %s login my system!" % username
loginSuccess = True
break
else:
f = file(lock_file,‘a‘)
f.write(‘‘%s\n‘ %username)
print "Entered 3 times of wrong passwd,going to lock %s" % username
if loginSuccess == True:break #jump out of the for top loop
if loginSuccess == True:break #jump out of the while loop
标签:password welcome correct 用户名 python
原文地址:http://sailq21.blog.51cto.com/6111337/1616814