Python 学习之登录注册

作者: ropon 分类: Python 发布时间: 2018-10-07 13:58
def register(userinfo, name, passwd):
    with open(userinfo, mode="r+", encoding="utf-8") as f1:
        for line in f1:
            # print(line.split("|")[0])
            if line.split("|")[0] == name:
                return 0,"{0}用户已注册,请更换其他用户名".format(name)
        f1.write(name+"|"+passwd+"\n")
        return 1,"注册成功,用户名是:{0},密码是:{1}".format(name,passwd)


def login(userinfo, name, passwd):
    with open(userinfo, mode="r", encoding="utf-8") as f1:
        for line in f1:
            # print(line.split("|")[0])
            # print(line.split("|")[1].strip())
            if  name == line.split("|")[0]:
                if passwd == line.split("|")[1].strip():
                    return 1,"用户名和密码正确,登录成功",0
                else:
                    return 0,"{0}用户名密码错误,请检查".format(name),"loginerror"
        return 0, "{0}用户名还没注册,请先注册".format(name),0

# flag = 1
# while flag:
#     name = input("请输入用户名:")
#     passwd = input("请输入密码:")
#     msg = register("userinfo", name, passwd)
#     flag = msg[0]
#     print(msg[1])

name = input("请输入用户名:")
count = 1
while count <=3:
    passwd = input("请输入密码:")
    msg = login("userinfo", name, passwd)
    if msg[0] == "1":
        print(msg[1])
        break
    else:
        if msg[2] == "loginerror":
            print(msg[1])
            print("已尝试{0}次,还剩余{1}次".format(count, 3 - count))
            count += 1
        else:
            print(msg[1])
            break

 

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!