python第一天,编写用户接口

作业:编写登陆接口

  • 输入用户名密码
  • 认证成功后显示欢迎信息
  • 输错三次后锁定

流程图:

  

代码

#!/usr/bin/env python
#-*- coding:utf-8 -*-
#created by 252513499@qq.com at 2018.11.21
from sys import argv
def ifuser(user,passwd):
    hash={}
#将文件内容以字典存储到内存中,hash[name] = ['password','err','lock']   
    F=open("/root/python/user.lock",'r+')
    for line in F:
        lists=line.strip().split()
        if len(lists) ==4:
            name =lists.pop(0)
            hash[name]=lists
    F.close()
    if user in hash.keys():
        while hash[user][2]!='yes':
            if(hash[user][0] == passwd):
#                *****写入文件,错误数更新为0**
                hash[user][1]=0
                F=open("/root/python/user.lock",'w')
                for name in hash.keys():
                    line=str(" ".join([name," ".join(map(str,hash[name]))]))
                    F.write(line+"
")
                F.close()
                print "欢迎您"+user
                break
            else:
                hash[user][1]=int(hash[user][1])+1
                if int(hash[user][1])<3:
                    print "您的密码错误,您还有"+str(3-hash[user][1])+"次机会,请重新输入密码"
#                    **写入文件,记录错误数**
                    F=open("/root/python/user.lock",'w')
                    for name in hash.keys():
                        line=str(" ".join([name," ".join(map(str,hash[name]))]))
                        F.write(line+'
')
                    F.close()
                    passwd=raw_input().strip()
                else:
#***********************写入文件,记录被锁定**************************
                    hash[user][2]='yes'
                    F=open("/root/python/user.lock",'w')
                    for name in hash.keys():
                        line=str(" ".join([name," ".join(map(str,hash[name]))]))
                        F.write(line+'
')
                    F.close()

        else:
#            ********************
            print "对不起,您的用户已经被锁定"
    else:
        print "对不起,您的用户不存在"
if __name__ == "__main__":
    if len(argv) != 3:
        print "
请输入正确的参数,例如
python 1.py user name"
    else:
        ifuser(argv[1],argv[2])

 

python 1.py user name

  

原文地址:https://www.cnblogs.com/students/p/10009318.html