'''
    个人博客项目开发
'''

import os, sys, time, shelve


def show_login():
    '''显示登录菜单'''
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t个人博客项目登录界面\t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t\t1. 用户登录\t\t\t* ~ * ~ *")
    print("* ~ * ~ *\t\t\t2. 用户注册\t\t\t* ~ * ~ *")
    print("* ~ * ~ *\t\t\t3. 退出系统\t\t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")

    choice = input("请输入选项:")

    return login_menu_dict.get(choice)() if choice in login_menu_dict else show_login()


def login():
    '''登录函数'''
    username = input("请输入账号(R/r返回): ")
    if username.upper() == 'R':
        return show_login()
    password = input("请输入密码: ")
    if username in users and users[username]['password'] == password:
        global online_user
        online_user = username
        return show_index()
    else:
        print("账号或密码有误,请重新输入!")
        time.sleep(1)
        return login()


def show_regist():
    '''显示注册菜单'''
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t个人博客项目注册界面\t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *请按提示信息,录入个人资料,完成注册* ~ * ~ *")
    print("-------------------------------------------------")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    time.sleep(1)
    return regist()


def regist():
    '''注册函数'''
    username = input("请输入注册账号(R/r返回): ")
    if username.upper() == 'R':
        return show_login()
    if username in users:
        print("用户账号已存在,请使用其他账号注册")
        time.sleep(1)
        return regist()
    password = input("请输入密码: ")
    confirm = input("请确认密码: ")
    if password != confirm:
        print("两次输入密码不一致,请重新注册")
        time.sleep(1)
        return regist()
    email = input("请输入(qq)邮箱: ")
    if not email.endswith("@qq.com"):
        print("邮箱格式有误,请重新注册")
        time.sleep(1)
        return regist()

    user = {'username' : username,'password' : password,'email' : email,'nickname' : 'None'}
    users[username] = user
    save_data()
    return show_login()

def show_index():
    '''展示主页菜单'''
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t个人博客项目主界面 \t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t1. 个人信息维护   \t\t* ~ * ~ *")
    print("* ~ * ~ *\t\t2. 文章信息维护   \t\t* ~ * ~ *")
    print("* ~ * ~ *\t\t3. 注销登录    \t\t* ~ * ~ *")
    print("* ~ * ~ *\t\t4. 退出系统    \t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")

    choice = input("请输入选项:")

    return index_menu_dict.get(choice)() if choice in index_menu_dict else show_index()


def userinfo_perfect():
    '''用户信息维护'''
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t 用户信息维护界面 \t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t1. 查询个人信息 \t\t* ~ * ~ *")
    print("* ~ * ~ *\t\t2. 修改登录密码 \t\t* ~ * ~ *")
    print("* ~ * ~ *\t\t3. 完善个人信息 \t\t* ~ * ~ *")
    print("* ~ * ~ *\t\t4. 返回上一级\t\t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")

    choice = input("请输入选项:")

    return userinfo_menu_dict.get(choice)() if choice in userinfo_menu_dict else userinfo_perfect()


def userinfo_look():
    '''查看个人信息'''
    global online_user
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t  个人信息界面  \t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("账号: ",users.get(online_user).get('username'))
    print("邮箱: ", users.get(online_user).get('email'))
    print("昵称: ", users.get(online_user).get('nickname'))
    for k,v in users.get(online_user).items():
        if k == 'usersex':
            print('性别: ',v)
        if k == 'userage':
            print('年龄: ',v)
        if k == 'userphone':
            print('手机: ',v)
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    input("任意键继续...")
    return userinfo_perfect()


def loginpass_modify():
    '''修改登录密码'''
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t  修改登录密码  \t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    oldpass = input("请输入原密码(R/r键返回): ")
    if oldpass.upper() == 'R':
        return userinfo_perfect()
    if oldpass != users[online_user]['password']:
        print("密码有误")
        time.sleep(1)
        return loginpass_modify()
    newpass = input("请输入新密码: ")
    confirm = input("请确认新密码: ")
    if newpass != confirm:
        print("两次输入密码不一致,请重新输入")
        time.sleep(1)
        return loginpass_modify()
    users[online_user]["password"] = newpass
    print("密码修改成功,请重新登录...")
    time.sleep(1)
    return show_login()


def userinfo_update():
    '''完善个人资料'''
    global online_user
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t  完善个人资料  \t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    if not len(users[online_user]['nickname']):
        print("账号: ", users.get(online_user).get('username'))
        print("邮箱: ", users.get(online_user).get('email'))
        print("昵称: 待完善..")
        print("性别: 待完善..")
        print("年龄: 待完善..")
        print("手机: 待完善..")
    else:
        print("账号: ", users.get(online_user).get('username'))
        print("邮箱: ", users.get(online_user).get('email'))
        print("昵称: ", users.get(online_user).get('nickname'))
        for k, v in users.get(online_user).items():
            if k == 'usersex':
                print('性别: ', v)
            if k == 'userage':
                print('年龄: ', v)
            if k == 'userphone':
                print('手机: ', v)
    print("请根据提示信息完成对应填写:")
    nickname = input("输入你的昵称: ")
    usersex = input("输入你的性别: ")
    userage = input("输入你的年龄: ")
    userphone = input("输入你的手机号: ")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    is_save = input("是否保存(y/n)?")
    if is_save != 'y':
        input("信息未发生变化,任意键退出...")
        return userinfo_perfect()

    user = {'username': online_user, 'password': users.get(online_user).get('password'), 'email': users.get(online_user).get('email'), 'nickname': nickname, 'usersex': usersex, 'userage': userage, 'userphone': userphone}
    users[online_user] = user
    save_data()
    input("信息修改成功,任意键继续...")
    return userinfo_perfect()


def articalinfo_perfect():
    '''文章信息维护'''
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t 文章信息维护界面 \t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t1. 发表文章\t\t\t* ~ * ~ *")
    print("* ~ * ~ *\t\t2. 删除文章\t\t\t* ~ * ~ *")
    print("* ~ * ~ *\t\t3. 修改文章\t\t\t* ~ * ~ *")
    print("* ~ * ~ *\t\t4. 查询个人文章\t\t* ~ * ~ *")
    print("* ~ * ~ *\t\t5. 查询所有文章\t\t* ~ * ~ *")
    print("* ~ * ~ *\t\t6. 发表评论\t\t\t* ~ * ~ *")
    print("* ~ * ~ *\t\t7. 回收站  \t\t\t* ~ * ~ *")
    print("* ~ * ~ *\t\t8. 返回上一级\t\t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")

    choice = input("请输入选项:")

    return articalinfo_menu_dict.get(choice)() if choice in articalinfo_menu_dict else articalinfo_perfect()


def artical_publish():
    '''发表文章'''
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t  发表博客文章  \t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    title = input("请输入文章标题(R/r键返回): ")
    if title.upper() == 'R':
        return articalinfo_perfect()
    if title in articals:
        print("标题已存在,请用其他标题")
        time.sleep(1)
        return artical_publish()
    content = ''
    for i in range(0,50):
        one_line = input("请输入文章内容(R/r结束):")
        if one_line.upper() == 'R':
            break
        else:
            content += one_line
    print(content)
    print("文章发表中...")
    artical = {'title' : title,'content' : content,'author' : online_user,'readed_count' : 0,'statue' : True,'comment' : comments}
    articals[title] = artical
    save_data()
    time.sleep(1)
    input("文章发表成功!任意键返回...")
    return articalinfo_perfect()


def artical_delete():
    '''删除文章'''
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t  删除博客文章  \t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    artical_loop(online_user)
    print("---------------------------------------------")

    title = input("请输入您要删除的博客文章标题(R/r键返回): ")
    if title.upper() == 'R':
        return articalinfo_perfect()
    else:
        if title in articals and articals.get(title).get('author') == online_user:
            articals[title]['statue'] = False
            input("文章删除成功,任意键返回...")
        else:
            input("未找到您要删除的文章,任意键继续...")
        return articalinfo_perfect()


def artical_modify():
    '''修改文章'''
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t  修改博客文章  \t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    artical_loop(online_user)
    print("---------------------------------------------")

    title = input("请输入您要修改的博客文章标题(R/r键返回): ")
    if title.upper() == 'R':
        return articalinfo_perfect()
    else:
        if title in articals and articals.get(title).get('author') == online_user:
            print(articals.get(title).get('content'))
            content = ''
            for i in range(0, 50):
                one_line = input("请修改文章内容(R/r结束):")
                if one_line.upper() == 'R':
                    break
                else:
                    content += one_line

            print("文章修改中...")
            articals[title]['content'] = content
            time.sleep(1)
            input("文章修改成功,任意键返回...")
        else:
            input("未找到您要修改的文章,任意键继续...")
        return articalinfo_perfect()


def artical_find_self():
    '''查询自己所有文章'''
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t 查看个人博客文章 \t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    artical_loop(online_user)
    print("---------------------------------------------")
    title = input("请输入您要查看的博客文章标题(R/r键返回): ")
    if title.upper() == 'R':
        return articalinfo_perfect()
    else:
        if title in articals and articals.get(title).get('author') == online_user:
            return artical_detail(title)
        else:
            print("您尚未发表此篇文章")
            time.sleep(1)
            return articalinfo_perfect()


def artical_find_all():
    '''查询所有文章'''
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t 查看所有博客文章 \t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ >标题\t\t作者")
    for k in articals.keys():
        if articals.get(k).get('statue'):
            print("* ~ * ~ >{}\t\t{}".format(k, articals.get(k).get('author')))
    print("---------------------------------------------")
    title = input("请输入您要查看的博客文章标题(R/r键返回): ")
    if title.upper() != 'R':
        if title not in articals:
            print("尚未发表此篇文章")
            time.sleep(1)
        else:
            return artical_detail(title)
    return articalinfo_perfect()


def comment_publish():
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t  评论博客文章  \t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ >标题\t\t作者")
    for k in articals.keys():
        if articals.get(k).get('statue'):
            print("* ~ * ~ >{}\t\t{}".format(k, articals.get(k).get('author')))
    print("---------------------------------------------")
    title = input("请输入您要评论的博客文章标题(R/r键返回): ")
    if title.upper() != 'R':
        if title in articals and articals.get(title).get('statue'):
            if articals.get(title).get('author') == online_user:
                print("不能评论自己的文章→→!")
                time.sleep(1)
                return comment_publish()
            else:
                print(articals.get(title).get('content'))
                print("---------------------------------------------")
                comment = input("请输入评论的内容: ")
                print("评论上传中...")
                if online_user in articals[title]['comment']:
                    # articals[title]['comment'][online_user].append(comment)
                    if title in articals[title]['comment'][online_user]:
                        articals[title]['comment'][online_user][title].append(comment)
                    else:
                        articals[title]['comment'][online_user][title] = [comment]
                else:
                    # articals[title]['comment'][online_user] = [comment]
                    articals[title]['comment'][online_user] = {title : [comment]}
                    print(articals[title]['comment'][online_user])
                time.sleep(1)
                input("评论发表成功,任意键返回...")
                # return articalinfo_perfect()
        else:
            print("此篇文章不存在,不能发布评论。")
            time.sleep(1)
    return articalinfo_perfect()


def artical_detail(title):
    '''文章详情'''
    artical = articals.get(title)
    rc = artical.get("readed_count")
    rc += 1
    artical['readed_count'] = rc
    print("文章标题: {}".format(artical.get('title')))
    print("文章作者: {}".format(artical.get('author')))
    print("文章内容: {}".format(artical.get('content')))
    print("阅读次数: {}".format(artical.get('readed_count')))
    if len(articals.get(title).get('comment')):
        # for c in articals.get(title).get('comment'):
        for u in articals.get(title).get('comment'):
            if title in articals.get(title).get('comment').get(u):
                print(u, articals.get(title).get('comment').get(u).get(title))
    input("任意键返回...")
    return articalinfo_perfect()


def artical_loop(online):
    '''遍历文章'''
    print("* ~ * ~ >标题\t\t作者")
    for k in articals.keys():
        if articals.get(k).get('statue') and articals.get(k).get('author') == online:
            print("* ~ * ~ >{}\t\t{}".format(k, articals.get(k).get('author')))


def recover_menu():
    '''回收站'''
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t    回收站     \t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t  1. 恢复删除的文章\t\t* ~ * ~ *")
    print("* ~ * ~ *\t\t  2. 清空回收站  \t\t* ~ * ~ *")
    print("* ~ * ~ *\t\t  3. 返回上一级  \t\t* ~ * ~ *")

    choice = input("请输入您的选项:")

    return recover_menu_dict.get(choice)() if choice in recover_menu_dict else recover_menu()


def artical_recover():
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t 恢复删除的文章  \t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ >标题\t\t作者")
    for k in articals.keys():
        if not articals.get(k).get('statue') and articals.get(k).get('author') == online_user:
            print("* ~ * ~ >{}\t\t{}".format(k, articals.get(k).get('author')))
    print("---------------------------------------------")
    title = input("请输入您要恢复的博客文章标题(R/r键返回): ")
    if title.upper() != 'R':
        if title in articals and articals.get(title).get('author') == online_user:
            articals[title]['statue'] = True
            input("恢复成功,任意键返回...")
        else:
            print("未找到你要恢复的文章")
            time.sleep(1)
    return articalinfo_perfect()


def delete_recover():
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t   清空回收站    \t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ >标题\t\t作者")
    for k in articals.keys():
        if not articals.get(k).get('statue') and articals.get(k).get('author') == online_user:
            print("* ~ * ~ >{}\t\t{}".format(k, articals.get(k).get('author')))
    print("---------------------------------------------")
    is_delete = input("是否清空回收站(y/n): ")
    if is_delete != 'y':
        return recover_menu()
    else:
        for k in articals.keys():
            if not articals.get(k).get('statue') and articals.get(k).get('author') == online_user:
                articals.pop(k)
                input("回收站已清空,任意键返回...")
                return recover_menu()


def save_data():
    '''保存数据到文件'''

    if not os.path.exists('./data'):
        os.mkdir('./data')
    file = shelve.open('./data/data')
    file['users'] = users
    file['articals'] = articals


def get_data():
    '''从文件提取数据到程序'''
    global users, articals
    if os.path.isdir('./data') and os.path.isfile('./data/data.dat'):
        file = shelve.open('./data/data')
        users = file['users']
        articals = file['articals']


def exit_system():
    '''退出系统'''
    for i in range(1,4):
        print("系统{}s后退出".format(4 - i))
        time.sleep(1)
    save_data()
    sys.exit(1)


users = dict()

articals = dict()

comments = dict()

online_user = None
#登录菜单选项函数对应关系
login_menu_dict = {
    '1' : login,
    '2' : show_regist,
    '3' : exit_system
}
#首页菜单选项函数对应关系
index_menu_dict = {
    '1' : userinfo_perfect,
    '2' : articalinfo_perfect,
    '3' : show_login,
    '4' : exit_system
}
#用户信息维护菜单选项函数对应关系
userinfo_menu_dict = {
    '1' : userinfo_look,
    '2' : loginpass_modify,
    '3' : userinfo_update,
    '4' : show_index
}
#文章信息维护菜单选项函数对应关系
articalinfo_menu_dict = {
    '1' : artical_publish,
    '2' : artical_delete,
    '3' : artical_modify,
    '4' : artical_find_self,
    '5' : artical_find_all,
    '6' : comment_publish,
    '7' : recover_menu,
    '8' : show_index
}
#回收站菜单选项函数对应关系
recover_menu_dict = {
    '1' : artical_recover,
    '2' : delete_recover,
    '3' : articalinfo_perfect,
}


def engine():
    '''初始化数据'''
    get_data()
    '''初始化引擎: 展示登录菜单'''
    show_login()


engine()

测试:


python博客如何写 python开发博客_用户信息

python博客如何写 python开发博客_提示信息_02

     

python博客如何写 python开发博客_提示信息_03

python博客如何写 python开发博客_用户信息_04

     

python博客如何写 python开发博客_用户信息_05

python博客如何写 python开发博客_用户信息_06

     

python博客如何写 python开发博客_python博客如何写_07

python博客如何写 python开发博客_用户信息_08

   

python博客如何写 python开发博客_用户信息_09

python博客如何写 python开发博客_提示信息_10

   

python博客如何写 python开发博客_提示信息_11

python博客如何写 python开发博客_用户信息_12

   

python博客如何写 python开发博客_个人博客_13

python博客如何写 python开发博客_个人博客_14

   

python博客如何写 python开发博客_python博客如何写_15

python博客如何写 python开发博客_提示信息_16

   

python博客如何写 python开发博客_个人博客_17

python博客如何写 python开发博客_个人博客_18

    

python博客如何写 python开发博客_个人博客_19

python博客如何写 python开发博客_个人博客_20

  

python博客如何写 python开发博客_提示信息_21

python博客如何写 python开发博客_python博客如何写_22