编写小说阅读程序实现下属功能
一:程序运行开始时显示
0 账号注册
1 充值功能
2 阅读小说
二: 针对文件db.txt,内容格式为:"用户名:密码:金额",完成下述功能
2.1、账号注册
2.2、充值功能
三:文件story_class.txt存放类别与小说文件路径,如下,读出来后可用eval反解出字典
{"0":{"0":["倚天屠狗记.txt",3],"1":["沙雕英雄转.txt",10]},"1":{"0":["令人羞耻的爱.txt",6],"1":["二狗的妻子与大草原的故事.txt",5]},}
3.1、用户登录成功后显示如下内容,根据用户选择,显示对应品类的小说编号、小说名字、以及小说的价格
"""
0 玄幻武侠
1 都市爱情
2 高效养猪36技
"""
3.2、用户输入具体的小说编号,提示是否付费,用户输入y确定后,扣费并显示小说内容,如果余额不足则提示余额不足
四:为功能2.2、3.1、3.2编写认证功能装饰器,要求必须登录后才能执行操作
五:为功能2.2、3.2编写记录日志的装饰器,日志格式为:"时间 用户名 操作(充值or消费) 金额"
附加:可以拓展作者模块,作者可以上传自己的作品
import time,os
time.strftime('%Y-%m-%d %X')
globals_name = None
def msg_log(func):
"""记录日志的装饰器"""
def wrapper(*args, **kwargs):
global globals_name
res = func(*args, **kwargs)
print(res)
with open('record.log', 'ab')as log_f:
log_f.write('时间:{} 用户:{} 操作:{} 金钱:{}元
'.format(time.strftime('%Y-%m-%d %X'), globals_name,res[0],res[1]).encode('utf-8'))
return res
return wrapper
def move_fuc():
"""操作系统相关操作"""
os.remove(r'{}'.format('db.txt'))
os.rename(r'{}.swap'.format('db.txt'), r'{}'.format('db.txt'))
def islogin(func):
"""编写认证功能装饰器"""
def wrapper(*args,**kwargs):
global globals_name
if not globals_name:
print('账号未登录')
login_func()
res = func()
else:
print('账号已经成功登录')
res = func()
return res
return wrapper
def regist_func():
'''账号注册'''
# 判断账号是否重复
print("账号注册".center(50, '='))
tag = 1
with open(r'db.txt','rb') as rf,
open(r'db.txt','ab') as af:
while tag:
name_user = input('请输入您的注册账号:>>').strip()
for line in rf:
x, *_ = line.decode('utf-8').strip().split(':')
if x == name_user:
print('账号重复,请重新输入')
break
else:
print('账号未重复,请放心注册')
tag = 0
else: # 注册
name_pwd = input('请输入您的注册密码:>>').strip()
name_money = input('请输入您的初始金钱:>>').strip()
af.write('
{}:{}:{}'.format(name_user,name_pwd,name_money).encode('utf-8'))
print("注册成功".center(50, '='))
def login_func():
'''账号登录'''
print("账号登录".center(50, '='))
name_user = input('请输入您的账号:>>').strip()
name_pwd = input('请输入您的密码:>>').strip()
with open(r'db.txt','rb')as f:
for line in f:
x,y,z = line.decode('utf-8').strip().split(':')
if name_user == x:
if y == name_pwd:
print("登录成功".center(50, '='))
global globals_name
globals_name = name_user
return
else:
print('账号密码错误')
return
else:
print('账号不存在,请注册')
regist_func()
login_func()
@msg_log
@islogin
def recharge_func():
'''账号充值'''
global globals_name
money = input('请输入您的充值金额:>>').strip()
if not money.isdigit():
print('输入的是钱,是数字,玩呢?')
return
with open(r'db.txt', 'rb') as rf,
open(r'db.txt.swap', 'wb') as wf:
while True:
user_msg = rf.readline().decode('utf-8')
if len(user_msg) == 0:
break
*_, z = user_msg.strip().split(':')
if globals_name in user_msg:
money1 = int(z) + int(money)
data = user_msg.replace('{}'.format(z), '{}'.format(money1))
wf.write(data.encode('utf-8'))
else:
wf.write('{}'.format(user_msg).encode('utf-8'))
move_fuc()
print("注册成功".center(50, '='))
print('充值成功,现有金额为{}元'.format(money1))
print(type(money))
return '充值',money
def pay_func(money):
'''付费功能'''
global globals_name
with open(r'db.txt', 'rb') as rf,
open(r'db.txt.swap', 'wb') as wf:
while True:
user_msg = rf.readline().decode('utf-8')
if len(user_msg) == 0:
break
*_, z = user_msg.strip().split(':')
if globals_name in user_msg:
money = int(z) - int(money)
data = user_msg.replace('{}'.format(z), '{}'.format(money))
wf.write(data.encode('utf-8'))
else:
wf.write('{}'.format(user_msg).encode('utf-8'))
move_fuc()
print("支付成功".center(50, '='))
print('支付成功,现有金额为{}元'.format(money))
return '支付成功'
@islogin
def reading_class():
'''小说分类'''
print("小说分类".center(50, '='))
print("""
0 玄幻武侠
1 都市爱情
2 高效养猪36技
""")
with open(r'story_class.txt', 'rb') as rf:
res = eval(rf.readline().decode('utf-8'))
while 1:
cmd = input("输入指令(序号)>>>:").strip()
if cmd in res:
reading_func(cmd)
break
else:
print('无效指令,请重新输入')
@msg_log
def reading_func(x):
global globals_name
with open(r'db.txt', 'rb') as rf:
while True:
user_msg = rf.readline().decode('utf-8')
if len(user_msg) == 0:
break
nam,pwd, z = user_msg.strip().split(':')
if globals_name == nam:
money = z
with open(r'story_class.txt', 'rb') as rf:
res = eval(rf.readline().decode('utf-8')).get(x)
msg = [res.get(item) for item in res]
x = 0
l = []
for k,v in msg:
print('编号{}: 书名:{} 价格:{}元'.format(x,k,v))
new_l = [x,k,v]
l.append(new_l)
x += 1
while True:
cmd = input("请选择要阅读的小说编号>>>:")
if not cmd.isdigit() or int(cmd) > x:
print("指令无效,请重新输入")
continue
num,name,price = l[int(cmd)]
print(num)
if num == int(cmd):
isok = input("是否付费(y)购买或任意键退出{}>>>:".format(name))
if isok == 'y':
print(price)
print(money)
if int(price) > int(money):
print('账号余额不足')
break
else:
print('购买成功')
pay_func(int(price))
print(name)
return '购买',price
else:
return
if __name__ == '__main__':
dic = {'0': regist_func,
'1': recharge_func,
'2': reading_class
}
print("欢迎来到皮皮猪小说网".center(50, '='))
while True:
print('''
0 账号注册
1 充值功能
2 阅读小说
''')
cmd = input("输入指令(序号)>>>:").strip()
if cmd in dic:
dic[cmd]()
else:
print("无效命令")
拓展未完成……