Python入门 Day3-博客登录程序

要求

1),启动程序,首页面应该显示成如下格式:
    欢迎来到博客园首页
    1:请登录
    2:请注册
    3:文章页面
    4:日记页面
    5:评论页面
    6:收藏页面
    7:注销
    8:退出程序
2),用户输入选项,3~6选项必须在用户登录成功之后,才能访问成功。
3),用户选择登录,用户名密码从register文件中读取验证,三次机会,没成功则结束整个程	序运行,成功之后,
可以选择访问3~6项.
4),如果用户没有注册,则可以选择注册,注册成功之后,可以自动完成登录,然后进入首页选择。
5),注销用户是指注销用户的登录状态,使其在访问任何页面时,必须重新登录。
6),退出程序为结束整个程序运行。

 

代码:

import time

##  登录状态
user_status = {'username': None,'status': False,}

dic1 = {
    1: '登录',
    2: '注册',
    3: '文章',
    4: '日记',
    5: '评论',
    6: '收藏',
    8: '退出程序'
}

dic2 = {
    3: 'artecle',
    4: 'diary',
    5: 'comment',
    6: 'collection',
    7: 'logout',
    8: 'exited',
}

Flag = True

def wrapper(f1): #装饰器 选择页面前判断是否登录,如果未登录 跳转至登录页面
    def inner(*args, **kwargs):
        if user_status.get('status'):
            ret = f1(*args, **kwargs)
            time.sleep(1)
            print('...\n\n请选择其他操作:\n')
            choice2()
            # return inner
        else:
            print('您还未登录,请登录您的账号')
            login()
    return inner

def login(): ##等登录接口,三次确认
    cot = 1
    while cot < 4:
        print('请登录')
        Usernm = input('Username:').strip()
        Passwd = input('Password:').strip()
        with open('register', encoding='utf-8') as f1:
            for line in f1:
                if Usernm == line.split()[0] and Passwd == line.split()[1]:
                    print('welcome,', Usernm)
                    cot = 4
                    global user_status
                    user_status = {'username': Usernm, 'status': True, }
                    return choice2()
            else:
                cot += 1
                if cot == 4:
                    Asw = input('错太多次了, Try again(Y|N)?')
                    if Asw == 'Y' or Asw == 'y':
                        cot = 1
                    else:
                        print("这么多次也不对?拜拜喽")
                        break
                else:
                    print('Your username or password is wrong,Try again')

def register(): # 注册用户,已存在用户直接跳转至登录接口
    print('请注册用户')
    new_user = input('用户名: ').strip()
    with open('register', encoding='utf-8', mode='r+') as f1:
        for line in f1:
            if new_user == line.split()[0]:
                print('该用户已注册,请直接登录')
                return login()
        else:
            new_pass = input('请输入密码: ').strip()
            f1.write('\n')
            f1.write(new_user)
            f1.write('\t')
            f1.write(new_pass)
            global user_status
            user_status = {'username': new_user, 'status': True, }
            choice2()

@wrapper #artecle  = wraaper(article)
def article():
    print('欢迎%s来到文章页面'%(user_status.get('username')))

@wrapper
def diary():
    print('欢迎来到日记本页面')

@wrapper
def comment():
    print('欢迎来到评论页面')

@wrapper
def collection():
    print('欢迎来到收藏页')

@wrapper
def logout():
    print('退出登录')
    global user_status
    user_status = {'username':None, 'status': False, }


def exited():  #退出程序
    print('退出程序')
    global Flag
    Flag = False


def choice2(): ## 登录后操作选项
    global user_status
    if user_status.get('status'):
        print('欢迎登陆,请按序号选择登录页面:')
        for k, v in dic2.items():
            print(k, v)
        choice = input('请按序号选择页面: ').strip()
        if choice.isdigit():
            choice = int(choice)
            if choice == 3:
                article()
            elif choice == 4:
                diary()
            elif choice == 5:
                comment()
            elif choice == 6:
                collection()
            elif choice == 7:
                logout()
            elif choice == 8:
                exited()
        else:
            print('请输入正确序号')


def main():

    while Flag: # 主函数
        print('请输入序号选择操作:')
        for k, v in dic1.items():
            print(k, v)
        choice = input('请选择操作项目:').strip()
        if  choice.isdigit():
            choice = int(choice)
            if choice == 1:
                login()
            elif choice == 2:
                register()
            elif choice == 3:
                article()
            elif choice == 4:
                diary()
            elif choice == 5:
                comment()
            elif choice == 6:
                collection()
            # elif choice == 7:
            #     pass
            elif choice == 8:
                exited()
            else:
                print('请按提示输入:')
        else:
            print('请按提示输入')
            return main()

main()

 

posted on 2018-04-20 22:35  鸿飞漫天  阅读(367)  评论(0编辑  收藏  举报

导航