# !/usr/bin/env python
# -*- coding -*-

#判断用户要注册的名字是否已经存在
def user_exits(username):
    with open('chen.txt', 'r+') as f:
        for line in f:
            #去掉这行前后的空格和换行符
            line = line.strip()
            #以$符号位标识,形成一个列表
            line_list = line.split('$')
            if line_list[0] == username:
                return True

        return False

#进行用户注册
def register(username, password):
    with open('chen.txt', 'a+') as f:
        #在文档的最后面追加一行,得注意加上换行符符号
        line = '\n' + username + "$" + password
        f.write(line)
        print("注册成功")

#这个是登陆函数
def login(username, password):
    with open('chen.txt','r+') as f:
        for line in f:
            line = line.strip()
            line_list = line.split('$')
            if line_list[0] == username and line_list[1] == password :
                print('登陆成功')
                return True
    print('登陆失败')
    return False

def main():
    number = input('1.登陆   2.注册')

    username = input("请输入用户名")
    password = input("请输入密码")
    if number == '2':
        isexits = user_exits(username)
        if isexits:
            print("已经存在")
        else:
            register(username, password)
    elif number == '1':
        login(username,password)
    else :
        print("请输入正确的选择")
main()