Learning python - fiction reader

Updated 23:41 ------------------------------ -------------- 3.26 -------------- 
# write fiction readers realize subordinate function
# # a: Shows when the program began running
# 0 account registration
# 1 top-up
# 2 read novels
#
#
# # two: for file db.txt, content format: "username: password: amount" to complete the following functions
# 2.1, account registration
# 2.2, recharge
#
# # three: file story_class.txt store category and fiction file path, as follows, available eval read out after the inverse solution dictionary
# { "0": { "0": [ "Heaven dog carcasses mind .txt", 3], "1 ": [ " heroes Shadiao turn .txt", 10]}, "1": { "0": [ "shameful love .txt", 6], "1 ": [ " his wife and two dogs prairie story .txt", 5]},}
#
# 3.1 after the user logs in successfully following information is displayed according to the user selection, the number corresponding to the category of fiction, fiction name, as well as novels price
# "" "
# 0 fantasy martial arts
# 1 urban love
# 2 efficient technology 36 pig
#" ""
#
# 3.2, the user enters a specific number of novels, you are prompted to pay for user input after determining y, deductions and display the content of the novel, if the balance is less than the balance of the lack of prompt
#
# # Four: After authentication function is the capability to write 2.2,3.1,3.2 decorator, requirements must be signed in to perform operations
#
# # V: write logging decorator to function 2.2,3.2, log format: "Time Username operation (prepaid or consumption) the amount of "
#
#
#
# # additional:
# # can expand module authors, authors can upload their work
#
#
Import Time

UserStatus = { 'username': None}


DEF auth (FUNC):
'' '
certification decorator function
: return:
'' '

DEF warpper (* args, ** kwargs):
IF UserStatus [' username '] None iS:
inp_name = iNPUT (' enter a username ')
inp_psd = iNPUT (' password ')
with open ( 'db.txt', 'rt ', encoding = 'utf-8') as rf:
userinfo = {}
for line in rf:
name, PSD, Remind = line.strip () Split ( ':').
UserInfo [name] = [PSD, int (Remind)]
IF inp_name Not in UserInfo:
Print ( 'user does not exist')
return
IF inp_name in UserInfo UserInfo and [inp_name] [0] == inp_psd:
Print ( 'successful login, the user dear {}' .format (inp_name))
UserStatus [ 'username'] = inp_name
RES = FUNC (* args, ** kwargs)
return RES
the else:
Print ( 'account password wrong')
return
the else:
RES = FUNC (* args, ** kwargs)
return RES
return wrapper


def record_log(func):
'''
日志记录功能装饰器
:return:
'''

def wrapper(*args, **kwargs):
res = func(*args, **kwargs)
with open('record.log', 'ab')as log_f:
op_time = time.time()
amount = func.amount
log_f.write('{} {} {} {}\n'.format(op_time, userstatus['username'], func.__name__, amount).encode('utf-8'))
return res

return wrapper


def register():
'''
注册功能
:return:
'''
with open('db.txt', 'rt', encoding='utf-8')as rf:
userinfo = {}
for line in rf:
name, PSD, Remind = line.strip () Split ( ':').
UserInfo [name] = [PSD, int (Remind)]
inp_name = INPUT ( 'Enter name registration')
inp_psd = INPUT ( 'password')
inp_name in UserInfo IF:
Print ( 'username already exists')
return
re_psd the iNPUT = ( 'enter the password again')
IF inp_psd == re_psd:
Print ( 'successful registration')
with Open ( 'db.txt', 'AT' , encoding = 'UTF-. 8') AS AF:
af.write ( '{}: {}: 0 \ n'.format (inp_name, inp_psd))
: the else
inconsistent print (' password twice ')
return


@record_log
@ the auth
DEF add_credit ():
'''
Recharge
: return:
'' '
Print (' Welcome recharge, dear} { '. The format (UserStatus [' username ']))
with Open (' db.txt ',' RT ', encoding =' UTF-. 8 ') AS RF :
UserInfo = {}
for RF Line in:
name, PSD, Remind = line.strip () Split ( ':').
UserInfo [name] = [PSD, int (Remind)]
the while True:
AMOUNT INPUT = ( 'Please input recharge amount ') .strip ()
the conf = iNPUT (' to determine {} {} users recharge it, determine the input Y / y, exit N / n'.format (userstatus [ 'username '], amount))
the conf == IF 'the Y' or the conf == 'Y':
Print ( 'successful recharge, recharge {} to have you' .format (AMOUNT))
UserInfo [UserStatus [ 'username']][1] += int(amount)
break
elif conf == 'N' or conf == 'n':
BREAK
with Open ( 'db.txt', 'wt', encoding = 'UTF-. 8') AS WF:
for in UserInfo name:
wf.write ( '{}: {}: {} \ n'.format (UserInfo [name], UserInfo [name] [0], UserInfo [name] [. 1]))

@record_log
@auth
DEF read ():
'' '
to read a novel function
: return:
' ''
Pass


DEF Zimbabwe Logout ():
'' '
Sign
: return:
' ''
UserStatus [ 'username'] = None
return


DEF MENU ():
fuc_dic = {
'0': ( 'registration function', Register),
'. 1': ( 'recharge', add_credit ),
'2 ': (' reading a novel ', Read),
'. 3 ': (' Logout ', Zimbabwe Logout)
}
= True Tag
the while Tag:
cmd = the INPUT ( '' '
0 account registered
a recharge
2 to read a novel
3 out
' '')
int (cmd)
IF not cmd.isdigit ():
Print ( 'Please enter the number')
IF cmd.isdigit ():
fuc_dic [cmd] [. 1] ()
IF cmd ==. 3:
Tag = False

MENU ()

Guess you like

Origin www.cnblogs.com/heirenxilou/p/12578475.html