文件操作,是一个存为字符串格式的登陆系统,有增删改查的功能,但不是我想要的。。。

def register(NAME, PWD, TEL):
    with open('userdate.txt', 'a', encoding='utf-8') as f:
        new = '&&'.join([NAME, PWD, TEL])
        f.write('\n' + str(new))
    return True


def same(NAME):
    with open('userdate.txt', 'r', encoding='utf-8') as f:
        for i in f:
            name = i.split('&&')[0]
            if name == NAME:
                return True
    return False


def login(NAME, PWD):
    with open('userdate.txt', 'r', encoding='utf-8') as f:
        for i in f:
            if NAME == i.split('&&')[0] and PWD == i.split('&&')[1]:
                return True
    return False


def change(NAME, TEL):
    with open('userdate.txt', 'r', encoding='utf-8') as f:
        new = ''
        for i in f:
            if NAME == i.split('&&')[0] and TEL == i.split('&&')[2]:
                newpwd = input('输入新的密码')
                i = i.replace(i.split('&&')[1], newpwd)
            new += i
        # else:
        #     main()
    with open('userdate.txt', 'w', encoding='utf-8') as f:
        f.write(new)
        return True


def delete(NAME, PWD):
    with open('userdate.txt', 'r', encoding='utf-8') as f:
        new = ''
        for i in f:
            if NAME == i.split('&&')[0] and PWD == i.split('&&')[1]:
                i = '\n'
            new += i
        with open('userdate.txt', 'w', encoding='utf-8') as f:
            f.write(new)
            return True


def main():
    print('1:注册,2:登录,3:变更,4:删除')
    choice = input('请选择').strip()
    if choice == '1':
        NAME = input('输入用户名').strip()
        if same(NAME):
            print('用户名已被占用,重新输入')
            main()
        else:
            PWD = input('输入密码').strip()
            TEL = input('输入电话').strip()
            if register(NAME, PWD, TEL):
                print('注册成功')
    if choice == '2':
        NAME = input('输入用户名').strip()
        PWD = input('输入密码').strip()
        if login(NAME, PWD):
            print('登录成功')
    if choice == '3':
        NAME = input('输入用户名').strip()
        TEL = input('输入电话').strip()
        if change(NAME, TEL):
            print('修改成功')
    if choice == '4':
        NAME = input('输入用户名').strip()
        PWD = input('输入密码').strip()
        if delete(NAME, PWD):
            print('删除成功')
    else:
        main()


main()

 

posted @ 2018-02-06 14:35  xusuns  阅读(173)  评论(0编辑  收藏  举报