作业9
程序员文章站
2022-05-04 19:44:33
作业:# 1、编写文件修改功能,调用函数时,传入三个参数(修改的文件路径,要修改的内容,修改后的内容)既可完成文件的修改import osdef change_file(src_file, old_data, new_data): ''' :param src_file: 修改的文件路径及文件名 :param old_data: 要修改的内容 :param new_data: 修改后的内容 :return: ''' if os.path.e...
作业:
# 1、编写文件修改功能,调用函数时,传入三个参数(修改的文件路径,要修改的内容,修改后的内容)既可完成文件的修改
import os
def change_file(src_file, old_data, new_data):
'''
:param src_file: 修改的文件路径及文件名
:param old_data: 要修改的内容
:param new_data: 修改后的内容
:return:
'''
if os.path.exists(src_file):
src_file = os.path.normpath(src_file)
with open(src_file, mode='rt', encoding='utf-8') as f_read:
data = f_read.read()
if old_data not in data:
print('要修改的内容不存在!')
return
res = data.replace(old_data, new_data)
with open(src_file, mode='wt', encoding='utf-8') as f_write:
f_write.write(res)
else:
print('请输入正确的文件路径!')
change_file(r'C:\Users\Bayer04\PycharmProjects\S15new\day12\a.txt', 'dsb', 'lnb')
# 2、编写tail工具
import time
def tail_f(src_file):
with open(src_file, mode='rb') as f:
f.seek(0, 2)
for line in f:
if len(line) == 0:
time.sleep(0.3)
else:
print(line.decode('utf-8'), end="")
# 3、编写登录功能
# 4、编写注册功能
# 5、编写用户认证功能
# 选做题:编写ATM程序实现下述功能,数据来源于文件db.txt
# 1、充值功能:用户输入充值钱数,db.txt中该账号钱数完成修改
# 2、转账功能:用户A向用户B转账1000元,db.txt中完成用户A账号减钱,用户B账号加钱
# 3、提现功能:用户输入提现金额,db.txt中该账号钱数减少
# 4、查询余额功能:输入账号查询余额
# 选做题中的选做题:登录功能
# 用户登录成功后,内存中记录下该状态,上述功能以当前登录状态为准,必须先登录才能操作
# 导入模块
import time
# 全局变量
login_user = None
user_dict = {}
# 次要功能函数定义区
# 1.获取用户密码
def get_user_dict():
with open('db.txt', mode='rt', encoding='utf-8') as f:
for line in f:
db_name, db_pwd, db_balance, db_error_times, db_locked_time = line.strip('\n').split(':')
user_dict[db_name] = [db_pwd, int(db_balance), int(db_error_times), float(db_locked_time)]
print(user_dict)
return user_dict
# 2.用户字典文件写入文件中
def user_data_write():
with open('db.txt', mode='wt', encoding='utf-8') as f:
for db_name1, db_info in user_dict.items():
db_pwd1, db_balance1, db_error_times1, db_locked_time1 = db_info
user_data = f'{db_name1}:{db_pwd1}:{db_balance1}:{db_error_times1}:{db_locked_time1}\n'
f.write(user_data)
# 3.是否继续的函数
def continue_or_quit():
'''
用于转账、充值、提现后,确认是否继续操作的函数
:return:
'''
while True:
cmd = input('请您输入Y/y继续,N/n取消并返回上级菜单>>>:').strip()
if cmd not in 'YyNn':
print('请输入合法的命令!')
continue
if cmd == 'Y' or cmd == 'y':
return 'continue--it'
elif cmd == 'N' or cmd == 'n':
return 'quit——it'
# 主要功能函数定义区域 1.注册 2.登录 3.充值 4.转账 5.提现 6.查询余额
def quit_it():
global tag
tag = False
pass
def register():
# 数据结构: username:pwd:balance:error_times:locked_time
# 先取出数据,存成字典格式,写入全局用户字典,用于判断用户以及写入数据
get_user_dict()
# 特殊字符限制:ASCII码中数字的十进制位区间:[48,57],大写英文的十进制位区间:[65,90],\
# 小写英文的十进制位区间:[97,122],下划线95
while True:
tag_register = False
inp_name = input('请输入用户名>>>:').strip()
if inp_name in user_dict.keys():
print('用户名已存在!')
continue
elif len(inp_name) not in range(6, 19):
print('用户名长度必须在6-18之间')
continue
for i in inp_name:
leagl_list = list(range(48, 58)) + list(range(65, 91)) + list(range(97, 123)) + list(range(95, 96))
if ord(i) not in leagl_list:
print('用户名必须是数字、大小写字母或下划线!')
tag_register = True
break
if tag_register:
tag_register = False
register()
while True:
inp_pwd = input('请输入您的密码>>>:').strip()
if len(inp_pwd) not in range(6, 19):
print('密码长度必须在6-18之间!')
continue
re_inp_pwd = input('请确认密码>>>:').strip()
if not inp_pwd == re_inp_pwd:
print('两次密码不一致,请重新输入')
continue
user_dict[inp_name] = [inp_pwd, 0, 0, 0]
user_data_write()
return
def login():
global login_user
get_user_dict()
while True:
if not login_user:
inp_name = input('请输入用户名>>>:').strip()
if inp_name not in user_dict.keys():
print('用户名不存在,请重新输入!')
continue
# 判断用户是否在锁定期,以及用户是否是锁定用户
elif time.time() < user_dict[inp_name][3]:
print('用户已被锁定!')
continue
elif time.time() > user_dict[inp_name][3]:
# 用户脱离锁定时间,将锁定时间刷新掉
user_dict[inp_name][3] = 0
user_data_write()
while True:
inp_pwd = input('请输入您的密码>>>:').strip()
if inp_pwd == user_dict[inp_name][0]:
user_dict[inp_name][2] = 0
# 登录成功后,刷新文件中的错误次数
user_data_write()
login_user = inp_name
print(login_user)
print('登录成功!')
return
else:
user_dict[inp_name][2] += 1
if user_dict[inp_name][2] == 3:
# 用户已输错密码三次,锁定时间增加,错误次数消除
user_dict[inp_name][3] = time.time() + 10
user_dict[inp_name][2] = 0
user_data_write()
print('密码输错三次,锁定10S')
break
else:
# 密码输错,错误次数刷入文件中
user_data_write()
print('您已输错{}次密码,三次后将被锁定10s'.format(user_dict[inp_name][2]))
break
else:
print('用户{0}已登录'.format(login_user))
pass
def recharge():
global login_user
get_user_dict()
while True:
if login_user:
recharge_num = input('请输入充值的金额>>>:').strip()
if not recharge_num.isdigit():
print('请输入数字!')
continue
recharge_num = int(recharge_num)
print(f'用户:{login_user},本次充值金额:{recharge_num}元')
tag_continue_or_quit = True
while tag_continue_or_quit:
verify_cmd = input('请您输入Y/y确认充值操作,N/n取消充值操作,输入其他金额>>>:').strip()
if verify_cmd not in 'YyNn':
print('请输入正确的命令!')
continue
if verify_cmd == 'Y' or verify_cmd == 'y':
user_dict[login_user][1] += recharge_num
user_data_write()
print(f'用户:{login_user},本次成功充值{recharge_num}元,账户余额{user_dict[login_user][1]}')
# 是否继续操作指令
res = continue_or_quit()
if res == 'quit——it':
return
elif res == 'continue--it':
tag_continue_or_quit = False
break
else:
print('正在取消本次充值操作,继续输入充值金额') # 可以删掉
break
else:
login()
print(login_user)
def transfer():
global login_user
get_user_dict()
while True:
if login_user:
user_transfer_in = input('请输入您要转给谁>>>:').strip()
if user_transfer_in not in user_dict.keys():
print('被转帐用户不存在')
continue
transfer_out_num = input('请输入转出的金额>>>:').strip()
if not transfer_out_num.isdigit():
print('请输入数字!')
continue
transfer_out_num = int(transfer_out_num)
if user_dict[login_user][1] < transfer_out_num:
print('余额不足,请重新输入金额或充值后再转账!')
continue
print(f'用户:{login_user},转出金额:{transfer_out_num}元')
tag_continue_or_quit = True
while tag_continue_or_quit:
verify_cmd = input('请您输入Y/y确认转账操作,N/n取消转账操作去输入其他金额>>>:').strip()
if verify_cmd not in 'YyNn':
print('请输入正确的命令!')
continue
if verify_cmd == 'Y' or verify_cmd == 'y':
user_dict[login_user][1] -= transfer_out_num
user_dict[user_transfer_in][1] += transfer_out_num
user_data_write()
print(f'用户:{login_user},本次成功转账{transfer_out_num}元,账户余额{user_dict[login_user][1]}')
# 是否继续操作指令
res = continue_or_quit()
if res == 'quit——it':
return
elif res == 'continue--it':
tag_continue_or_quit = False
break
else:
print('正在取消本次转账操作,继续输入转账金额') # 可以删掉
break
else:
login()
print(login_user)
def withdraw():
global login_user
get_user_dict()
while True:
if login_user:
withdraw_num = input('请输入提现的金额>>>:').strip()
if not withdraw_num.isdigit():
print('请输入数字!')
continue
withdraw_num = int(withdraw_num)
if user_dict[login_user][1] < withdraw_num:
print('提现金额大于余额!请重新输入')
print(f'用户:{login_user},本次提现金额:{withdraw_num}元')
tag_continue_or_quit = True
while tag_continue_or_quit:
verify_cmd = input('请您输入Y/y确认提现操作,N/n取消提现操作,输入其他金额>>>:').strip()
if verify_cmd not in 'YyNn':
print('请输入正确的命令!')
continue
if verify_cmd == 'Y' or verify_cmd == 'y':
user_dict[login_user][1] -= withdraw_num
user_data_write()
print(f'用户:{login_user},本次成功提现{withdraw_num}元,账户余额{user_dict[login_user][1]}')
# 是否继续操作指令
res = continue_or_quit()
if res == 'quit——it':
return
elif res == 'continue--it':
tag_continue_or_quit = False
break
else:
print('正在取消本次提现操作,继续输入提现金额') # 可以删掉
break
else:
login()
print(login_user)
def check_balance():
global login_user
get_user_dict()
while True:
if not login_user:
login()
continue
print(f'用户:{login_user},账户余额:{user_dict[login_user][1]}元')
while True:
cmd = input('输入Y/y立即返回上级菜单>>>:').strip()
if cmd == 'Y' or cmd == 'y':
return
else:
print('命令不正确,请重新输入!')
continue
# 主功能面板
func_dict = {
'0': ['退出功能', quit_it],
'1': ['注册功能', register],
'2': ['登录功能', login],
'3': ['充值功能', recharge],
'4': ['转账功能', transfer],
'5': ['提现功能', withdraw],
'6': ['查询余额', check_balance],
}
tag = True
while tag:
print('=' * 25)
for k, v in func_dict.items():
print('{k} {v}'.format(k=k, v=v[0]))
func_cmd = input('请输入您要选择的功能>>>:').strip()
if func_cmd not in func_dict.keys():
print('输入的命令非法,请重新输入!')
continue
elif func_cmd in func_dict.keys():
func_dict[func_cmd][1]()
本文地址:https://blog.csdn.net/m0_49141258/article/details/107434940
上一篇: 坐拥数百土豪客户,60元一盒苹果卖不掉
下一篇: 传统电商如何做移动互联网营销?