#!/usr/bin/env python     
# -*- coding:utf-8 -*-      
import hashlib
import getpass
import sys
def login(name,password):
    file=open("user",'r',encoding='utf-8')
    for user in  file:
        list=user.split(":")
        if list[0] == name and list[1] == password:
            file.close()
            return True
        else:
            file.close()
            return False


def reg(name,password):
    file=open("user",'a',encoding="utf-8")
    info=name+":"+password+"\n"
    file.write(info)
    file.close()


def main():
    global user,pwd
    chonice=input("-----------(选择)1登录/2注册--------:")
    if chonice == "1":
        obj=hashlib.md5()
        user=input("输入您的用户名:")
        pw=getpass.getpass("输入您的密码:")
        obj.update(bytes(pw,encoding='utf-8'))
        pwd=obj.hexdigest()
        for times in range(3):
            if login(user,pwd) == True:
                print("登录成功!!")
                break
            else:
                print("第%d次错误!!!"%(times+1))
                if times == 2:
                    print("已超过3次,用户已禁止登录!!")
    elif chonice == "2":
        reg(user,pwd)
    else:
        print("选择错误退出!!")
        sys.exit(0)
main()