下面展示一些 内联代码片
。
====================本周选做作业如下====================
编写小说阅读程序实现下属功能
# 一:程序运行开始时显示
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 os
import time
def Registration():#用户注册
while True:
user_name = input("请输入账号:")
user_passworld = input("请输入密码:")
with open('db.txt', 'r+', encoding='utf-8') as f_rade:
print("你好")
res = (1 for i in f_rade if i.strip('\n').split(':')[0] == user_name)
res = sum(res)
if res == 0:
f_rade.write(str(user_name)+':'+str(user_passworld)+':'+'0000.00'+'\n')
print("账户已注册成功")
break
else:
print("用户已存在,注册失败,请选择是否继续"
"y:继续"
"n:结束")
choice = input("请给出你的选择:")
if choice == 'n':
break
else:
print("重新注册")
def outer(logging_type = '充值'):
def loging(func):#日志
def warpper(*args, **kwargs):
result = func(*args, **kwargs)
if result == False:
with open("loging.txt",'a',encoding='utf-8') as f_write:
str_info = time.strptime('%Y-%m-%d %X') + ' ' + user_state['name'] + logging_type + ":" + '充值失败'+'\n'
f_write.write(str_info)
return result
else:
with open("loging.txt",'a',encoding='utf-8') as f_write:
str_info = time.strftime('%Y-%m-%d %X') + ' ' + user_state['name'] + logging_type + ":" + str(result)+'\n'
f_write.write(str_info)
return True
return warpper
return loging
def auth(func):#认证
def warpper(*args,**kwargs):
global user_state
if user_state['name'] == None:
user_name = input("请输入账号:")
user_passworld = input("请输入密码:")
with open('db.txt', 'r+', encoding='utf-8') as f_rade:
print("你好")
res = (1 for i in f_rade if
i.strip('\n').split(':')[0] == user_name and i.strip('\n').split(':')[1] == user_passworld)
res = sum(res)
if res == 1:
user_state['name'] = user_name
result = func(*args, **kwargs)
return result
else:
print("账户登录失败:")
else:
result = func(*args, **kwargs)
return result
return warpper
@auth
@outer(logging_type = '充值')
def Recharge():#充值
global user_state
while True:
pay_gold = input("请输入要充值值的金额,傻逼:")
if pay_gold.isdigit():
pay_gold = float(pay_gold)
with open('db.txt','r',encoding='utf-8') as f_rade\
,open('temporary_db.txt', 'w', encoding='utf-8') as f_write:
print("第一步:")
for i in f_rade:
print("第2步:")
res = i.strip('\n').split(':')
if user_state['name'] == res[0]:
gole_sum = str(float(res[2]) + pay_gold)
res = res[0] + ':' + res[1] + ':' + gole_sum + '\n'
f_write.write(res)
print("%s的余额已经增加:%s,总金额现在是:%s" %(str(user_state['name']), str(pay_gold),gole_sum))
else:
f_write.write(i)
os.remove('db.txt')
os.rename('temporary_db.txt','db.txt')
return float(pay_gold)
else:
choice = input("金额输入不合法是否要重新输入Y/N:")
if choice == 'Y' or choice == 'y':
continue
else:
return False
@auth
def TxTReader():#阅读
f_rade=open('story_class.txt','r',encoding="utf-8")
dic_radebook = eval(f_rade.read())
f_rade.close()
del f_rade
print(dic_radebook,type(dic_radebook))
while True:
print("""
0 玄幻武侠
1 都市爱情
2 高效养猪36技
""")
option = input("请选择要看的种类:")
if option.isdigit():
for i , j in dic_radebook[option].items():
print("编号%s——》《%s》:%d元"%(i,j[0],j[1]),end='\n')
book_option = input("请选择要看的书本编码:")
print("《%s》书本需要:%s元"%(dic_radebook[option][book_option][0],dic_radebook[option][book_option][1]))
confirm_the_payment = input('y/n要购买,购买才能阅读:')
if confirm_the_payment == 'y' or confirm_the_payment == 'Y':
print("第一步")
res = close_an_account(dic_radebook[option][book_option][1])#dic_radebook[option][book_option][1]
print(res)
if res == True:
with open(dic_radebook[option][book_option][0] , 'r' ,encoding='utf-8') as f_rade:
print("任意按钮继续,quit退出")
while True:
is_continue = input("")
if is_continue == 'quit':
break
else:
try:
print(f_rade.__next__())
except:
break
else:
break
else:
break
else:
print("输入不合法,请重新输入!!!")
@auth
@outer(logging_type = '结算')
def close_an_account(amount_of_money):#结算
with open('db.txt', 'r', encoding='utf-8') as f_rade \
, open('temporary_db.txt', 'w', encoding='utf-8') as f_write:
count = 0
for i in f_rade:
res = i.strip('\n').split(':')
if user_state['name'] == res[0]:
if float(res[2]) >= amount_of_money:
count+=1
gold_sum = str(float(res[2]) - amount_of_money)
res = res[0] + ':' + res[1] + ':' + gold_sum + '\n'
f_write.write(res)
else:
f_write.write(i)
if count == 0:
print("余额不足请充值")
return False
else:
os.remove('db.txt')
os.rename('temporary_db.txt', 'db.txt')
return float(amount_of_money)
user_state = {'name':None}
func_tools = {0:Registration,1:Recharge,2:TxTReader}
while True:
"""
程序一运行就执行此选项代码,
账号注册成功,调用其他功能,输入一次账号就可以。
退出账号后可以在登录
"""
print("0 账号注册\n"
"1 充值功能\n"
"2 阅读小说\n"
"3 账号退出\n")
res = input("请选择:")
if type(int(res)) == int:
if 0 <= int(res) <= 2 :
func_tools[int(res)]()
else:
print("数据选择错误——》请重新输入")
else:
print("字符类型错误:——》请重新输入")