#!/usr/bin/env python import sys name = '' pw='' name_num = 0 pw_num = 0 #black_list = [] with open('a.txt','r') as f: for i in f.xreadlines(): namelist.append(i.split()[0]) pwlist.append(i.split()[1]) def name_test(input_name, namelist): if input_name in namelist: return True else: return False name = raw_input('Please input your name:') name_num +=1 while True: if name_test(name, namelist): break else: if name_num < 3: name = raw_input('Name is error,Please input your name again:') name_num += 1 else: print 'Sorry,Today you have mistyped user name three times' sys.exit() pw = raw_input('Please input your password:') pw_num +=1 while True: index_pw = pwlist[namelist.index(name)] if pw == index_pw: print 'welcome' break else: if pw_num < 3: pw = raw_input('Passwd is error,Please input your passwd again:') pw_num +=1 else: print 'Sorry,Today you have to lose the wrong password three times' sys.exit() 这里的a.txt的格式是
bao 123 zhang 456 后面还要加入黑名单的一些小程序 这里还有一个朋友给我的程序,感觉他的更加的简练,看着舒服:
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2016/7/29 11:46 # @Author : Big_Bao import sys def name_test(input_name, namelist): if input_name in namelist: return True else: print 'Name is None.' return False def pw_test(pw ,index_pw, innerloopnumber=0): if pw == index_pw: print 'Welcome.' inn = innerloopnumber+1 return inn else: print 'Password is None.' namelist = [] pwlist = [] with open('a.txt','r') as f: for i in f.xreadlines(): namelist.append(i.split()[0]) pwlist.append(i.split()[1]) index_pw = [] name = '' pw='' name_num = 0 pw_num = 0 inloopnum = 0 name = raw_input('Please input your name:') name_num +=1 while True: if name_test(name, namelist): index_pw = pwlist[namelist.index(name)] pw = raw_input('Please input your password:') pw_num +=1 inloopnum_ = pw_test(pw, index_pw,innerloopnumber=inloopnum) else: name = raw_input('Please input your name again:') name_num +=1 if name_num ==3: print 'You have no chance.' sys.exit() if pw_num==3: print 'You have no chance' sys.exit() if inloopnum_==1: sys.exit() 感谢这位远在成都的朋友平时的指点