Python作业-登录接口编写
#!/usr/bin/python # -*- coding: UTF-8 -*- #By Egrep QQ:10206334 #Blog http://www.cnblogs.com/egrep/ dic = { 'user1':{'password':'123','count':0}, 'user2':{'password':'456','count':0}, 'user3':{'password':'789','count':0}, } temp = True count = 0 while temp: user = input('login:') if user not in dic: #判断用户是否存在,不存在为真,提示用户'no user',否则执行else,判断用户账户累积错误次数,登录账户3次错误,退出程序,并且提示. print('no user') count+=1 if count >= 3: print('Too many attempts to log in') break else: password = input('password:') #用户操作则进行输入密码验证 if password == dic[user]['password']: #密码正确择登录进入下一个循环,输入命令 print('login success...') while temp: command=input('[root@test ~]#') #输入命令 if not command: continue if command == 'quit'or command == 'exit'or command == 'q': #判断是否输入退出指令 跳出循环 print('Exit procedure') break else: #打印输出命令 print(command) temp=False #如果上面指令是退出,这temp条件变成False else: dic[user]['count'] += 1 #密码输错count+=1 输错密码只有三次机会 if dic[user]['count'] > 2: #判断用户密码错误次数是否小于3次 print('The account has been locked.') #是的话,打印出用户已经锁定 while True: jixu = input('If you continue to enter Y, or y exit please enter q or quit:') #提示用户如果继续,请输入Y ,或y,退出请输入q或者quit if jixu == 'Y' or jixu == 'y': #判断用户是否继续输入密码,Y,count=0 继续开始循环 dic[user]['count']=0 count = 0 break elif not jixu: continue elif jixu == 'q'or jixu == 'quit': #判断用户是否退出程序,q退出程序 temp = False print('Exit procedure') break else: print('user or password error....') #提示用户密码错误 #程序只提供学习参考