您的位置:首页 > 其它

1/11学习总结(个人简易博客开发)

2019-01-11 21:29 295 查看

今日主要做了一个简单的博客,实现了用户注册、登陆,文章的增删改查。
----------------------------------------手动分割线-------------------------------------------
主要实现了以下内容:
模拟简单的博客
1.账号注册,登陆验证
2.写文章,包含【标题,作者,时间,内容】
3.查询文章,列出所有文章及时间
4.可查看其中一篇文章
5.删除文章
6.修改文章
7.退出系统

附上完整代码:
此代码个人认为难点在于
1.初学者对于多函数的使用
2.逻辑处理是否最简单效率最高
3.注意某些地方使用的全局变量
4.大量的变量命名一定要有意义和可读性 否则后面调用时 十分麻烦

import os,time,sys,json,time
from datetime import datetime

#读取用户信息
def r_user_message(path="user_message.json"):
if os.path.exists(path):  #存在 读取信息
with open(path,mode="r",encoding="gbk") as f_r:
return json.load(f_r)
else:   #不存在  创建一个
with open(path, mode="w", encoding="gbk") as f_w:
user_message = {"user": [{"account": "admin", "pwd": "123"}]}
json.dump(user_message, f_w)
return user_message
#更新写入用户个人信息
def w_user_message(user_message,path="user_message.json"):
with open(path,mode="w") as f_w:
json.dump(user_message,f_w)
#建立默认文章
def r_content_default(name,path="Content"):
if os.path.exists(path):
with open(os.path.join(path,name+".json"),mode="w") as f_w:
Blog_message = {"article":
[
{
"title": "default",
"author": name,
"time": datetime.now().strftime("%Y{}%m{}%d{}  %H:%M:%S").format("年", "月", "日"),
"content": "第一篇博客"
}
]
}
json.dump(Blog_message,f_w)
else:
os.mkdir(path)
if os.path.exists(path):
with open(os.path.join(path, name + ".json"), mode="w") as f_w:
Blog_message = {"article":
[
{
"title": "default",
"author": name,
"time": datetime.now().strftime("%Y{}%m{}%d{}  %H:%M:%S").format("年", "月", "日"),
"content": "第一篇博客"
}
]
}
json.dump(Blog_message, f_w)
# 注册界面
def user_register():
os.system("cls")
while True:

print("完善信息".center(50,"*"))
print("请输入账户:")
user_input_account = input("")
print("请输入密码:")
user_input_pwd = input("")
temp=r_user_message()    #写入所有账户信息
user_temp_input={"account":user_input_account,"pwd":user_input_pwd}
# 判断 账户是否合法
isX=False  #账号不存在的情况
for i in temp["user"]:
if i["account"]==user_input_account:

print("该用户名已经存在,注册失败!")
isX=True
break
if isX==False:    #不存在的话 写入账户信息
temp["user"].append(user_temp_input)
w_user_message(temp)
r_content_default(user_input_account)

#人性化加载界面
for i in range(1, 11):
print("正在加载~")
print(("-" * i) + "->", "%s%%" % (i * 10))
if i == 10:
time.sleep(0.5)
time.sleep(0.2)
os.system("cls")
os.system("cls")
main_style()
#登陆界面
def user_login():
global active_user
while True:
# 登陆界面

print("请输入账户名:")
user_input_account = input("")
print("请输入密码:")
user_input_pwd = input("")
# 检测用户输入账户密码是否正确
isY=False    #假设账户不存在
temp=r_user_message()
for i in temp["user"]:
if user_input_account==i["account"] and user_input_pwd==i["pwd"]:
print("登陆成功")
active_user=user_input_account

#人性化进入界面信息
os.system("cls")
for i in range(1, 11):
print("正在加载个人信息,请稍等"+("."*int(i//3)))
print(("-" * i) + "->", "%s%%" % (i * 10))
if i == 10:
time.sleep(1)
time.sleep(0.2)
os.system("cls")
os.system("cls")
isY = True
return isY
if isY==False:
print("用户名或者密码错误,请重新输入:")
# user_login()
#写入文章
def w_content(name,all_article,path="Content"):
with open(os.path.join(path,name+".json"),"w") as f_w:
json.dump(all_article,f_w)
print("保存成功!")
#读取文章
def r_content(name,path="Content"):
with open(os.path.join(path,name+".json")) as f_r:
return  json.load(f_r)
#写文章
def user_w_article(name):
print("编辑窗".center(50,"*"))
temp_title=input("请输入标题:")
temp_content=input("请输入内容:")
temp= {
"title":temp_title,
"author":name,
"time": datetime.now().strftime("%Y{}%m{}%d{}  %H:%M:%S").format("年", "月", "日"),
"content":temp_content
}
all_article=r_content(name)
all_article["article"].append(temp)
#更新文章
w_content(name,all_article)
#查看文章
def find_all_article(name):
all_article=r_content(name)
if len(all_article["article"])!=0:
for i in range(len(all_article["article"])):
print(i+1,"标题:%s\n时间:%s"%(all_article["article"][i]["title"],all_article["article"][i]["time"]))
else:
print("无文章")
#查看一篇文章详细内容
def find_one_article(name):
one_article=r_content(name)
if len(one_article["article"])!=0:
for i in range(len(one_article["article"])):
print(i+1,"标题:%s\n时间:%s"%(one_article["article"][i]["title"],one_article["article"][i]["time"]))
while True:
print("您需要查看哪一篇文章:")
user_inputA=int(input(""))
if user_inputA<=len(one_article["article"]):

print("标题:",(one_article["article"][user_inputA-1]["title"]))
print("作者:", (one_article["article"][user_inputA - 1]["author"]))
print("时间:", (one_article["article"][user_inputA - 1]["time"]))
print("内容:", (one_article["article"][user_inputA - 1]["content"]))
else:
print("输入有误,请重新输入。")
break
else:
print("无文章")
#修改文章
def fix_article(name):
all_article=r_content(name)
for i in range(len(all_article["article"])):
print(i+1,"标题:%s\n时间:%s"%(all_article["article"][i]["title"],all_article["article"][i]["time"]))
if len(all_article)>0:
while True:
print("您需要修改哪一篇文章:")
user_inputA=int(input(""))
if user_inputA<=len(all_article["article"]):

print("编辑窗".center(50, "*"))
temp_fixA_=input("请输入标题:")
temp_fixB=input("请输入内容:")
#临时文章
temp= {
"title":temp_fixA_,
"author":name,
"time": datetime.now().strftime("%Y{}%m{}%d{}  %H:%M:%S").format("年", "月", "日"),
"content":temp_fixB
}
all_article["article"][user_inputA-1]=temp
#更新文章
w_content(name,all_article)
else:
print("输入有误,请重新输入。")
break
else:
print("无文章,请编辑。")
#删除文章
def del_article(name):
all_article=r_content(name)
for i in range(len(all_article["article"])):
print(i+1,"标题:%s\n时间:%s"%(all_article["article"][i]["title"],all_article["article"][i]["time"]))

if len(all_article["article"])!=0:
while   True:
print("您需要删除哪一篇文章:")
user_inputA = int(input(""))
if user_inputA<=len(all_article["article"]):
user_inputA=int(user_inputA)
all_article["article"].pop(user_inputA-1)
#更新信息
w_content(name,all_article)
print("删除成功。")
else:
print("输入有误,请重新输入。")
break
else:
print("没有文章,无法删除。")
# main_style()
#主界面
def main_style():
global active_user
while True:
os.system("cls")
print("欢迎来到飞鱼个人博客".center(50,"*"))
print("\n","1.登陆".center(55," "),"\n")
print("\n","2.注册".center(55," "),"\n")
print("\n","3.退出".center(55," "),"\n")
print("*" * 60)
print("\n现在是北京时间:%s" %datetime.now().strftime("%Y{}%m{}%d{}  %H:%M:%S").format("年", "月", "日"))

user_inputX=input("\n\n请输入你的选择:")
if user_inputX == "1":
if user_login():
while True:
print("欢迎你:%s" % active_user)
print("\n现在是北京时间:%s" %datetime.now().strftime("%Y{}%m{}%d{}  %H:%M:%S").format("年", "月", "日"))
print("\n现在你想做一点什么?\n")
print("1.写文章\t  2.查看文章\t  3.查看一篇文章\t4.修改文章\t5.删除文章\t6.退出登陆")
user_input = input("")
if user_input == "1":
os.system("cls")
user_w_article(active_user)
elif user_input == "2":
os.system("cls")
find_all_article(active_user)
elif user_input == "3":
os.system("cls")
find_one_article(active_user)
elif user_input == "4":
os.system("cls")
fix_article(active_user)
elif user_input == "5":
os.system("cls")
del_article(active_user)
elif user_input == "6":
os.system("cls")
active_user=""
break
elif user_inputX=="2":
user_register()
elif user_inputX=="3":
sys.exit()
active_user=""         #全局  当前用户
main_style()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐