2020Python练习13——函数对象和闭包函数(二)
项目操练——ATM
@2020.3.21
# 编写ATM程序实现下述功能,数据来源于文件db.txt # 0、注册功能:用户输入账号名、密码、金额,按照固定的格式存入文件db.txt # 1、登录功能:用户名不存在,要求必须先注册,用户名存在&输错三次锁定,登录成功后记录下登录状态(提示:可以使用全局变量来记录) 下述操作,要求登录后才能操作 # 1、充值功能:用户输入充值钱数,db.txt中该账号钱数完成修改 # 2、转账功能:用户A向用户B转账1000元,db.txt中完成用户A账号减钱,用户B账号加钱 # 3、提现功能:用户输入提现金额,db.txt中该账号钱数减少 # 4、查询余额功能:输入账号查询余额
import os # 登陆 def login(): count = 0 while True: inp_user = input("账号:").strip() if os.path.exists(r'locked\{}.txt'.format(inp_user)): print("该账号已被锁定") count = 0 continue inp_pwd = input("密码:").strip() msg = login_register_file(0, user=inp_user, pwd=inp_pwd) if msg: global login_user login_user = inp_user return print("登录成功") else: if msg is None: print("该账号不存在") else: if count == 2: print("错误次数过多,账号已被锁定") with open(r'locked\{}.txt'.format(inp_user), 'wt', encoding='utf-8') as f: f.write('') continue print("密码错误,剩余次数:{}".format(2-count)) count += 1 # 注册 def register(): while True: inp_user = input("账号:").strip() inp_pwd = input("密码:").strip() inp_pwd2 = input("确认密码:").strip() inp_money = input("金额:").strip() if not (inp_user or inp_pwd or inp_pwd2 or inp_money): print("输入不能为空") continue if inp_pwd != inp_pwd2: print("两次密码不一致") continue if login_register_file(1, user=inp_user, pwd=inp_pwd, money=inp_money): return print("注册成功") else: print("账号已存在") # 登录注册功能实现函数 def login_register_file(acount, **kwargs): with open(r'db.txt', 'at', encoding='utf-8') as w, \ open(r'db.txt', 'rt', encoding='utf-8') as r: if acount == 0: r.seek(0, 0) for line in r: user, pwd,*_ = line.strip().split(':') if kwargs['user'] == user and pwd: if kwargs['pwd'] == pwd: return True else: return False else: return None if acount == 1: if r: r.seek(0, 0) for line in r: is_user, *_ = line.strip().split(':') if kwargs['user'] == is_user: return False else: w.write('{}:{}:{}\n'.format(kwargs['user'], kwargs['pwd'], kwargs['money'])) return True else: w.write('{}:{}:{}\n'.format(kwargs['user'], kwargs['pwd'], kwargs['money'])) return True # 充值 def recharge(): inp_money = input("请输入充值金额:").strip() msg = func_file('recharge', money=int(inp_money)) if msg == 200: print("充值成功") else: print("充值失败") # 转账 def transfer(): inp_other = input("请输入对方账号:").strip() inp_money = input("请输入转账金额:").strip() if not is_user(inp_other): print("对方账号不存在") msg = func_file('transfer', other_user=inp_other, money=int(inp_money)) if msg == 200: print("转账成功") else: print(msg) # 提现 def withdraw(): inp_money = input("请输入提取金额:").strip() msg = func_file('withdraw', money=int(inp_money)) if msg == 200: print('提现成功') else: print(msg) # 查询余额 def balance(): inp_user = input("请输入你的账号:").strip() if inp_user == login_user: money = func_file('balance', user=inp_user) print("【{}】,您的余额为:{}元".format(login_user, money)) else: print("账号错误") def is_user(other_user): with open(r'db.txt', 'rb') as r: # 判断对方用户是否存在 for line in r: user, *_ = line.decode('utf-8').strip().split(':') if other_user == user: return True else: return False # 功能函数 def func_file(acount, **kwargs): with open(r'db.txt', 'rb') as r,\ open(r'db.txt.swap', 'wb') as w: # 查询余额 if acount == 'balance': while True: user, *_, money = r.readline().decode('utf-8').strip().split(':') if kwargs['user'] == user and money: return money while True: line = r.readline() if len(line) == 0: break user, pwd, old_money = line.decode('utf-8').strip().split(':') old_money = int(old_money) if login_user == user and pwd and old_money: # 充值 if acount == 'recharge': my_money = old_money + kwargs['money'] else: # 转账以及提取金额 if old_money >= kwargs['money']: my_money = old_money - kwargs['money'] else: os.remove('db.txt.swap') return '转账失败,余额不足' w.write('{}:{}:{}\n'.format(login_user, pwd, my_money).encode('utf-8')) else: # 转账后对方账户余额 if kwargs.get('other_user') == user and pwd and old_money: user_money = old_money + kwargs['money'] w.write('{}:{}:{}\n'.format(kwargs.get('other_user'), pwd, user_money).encode('utf-8')) else: w.write('{}:{}:{}\n'.format(user, pwd, old_money).encode('utf-8')) os.remove(r'db.txt') os.rename('db.txt.swap', 'db.txt') return 200 login_user = None func_dic={ '1': ('充值', recharge), '2': ('转账', transfer), '3': ('提现', withdraw), '4': ('余额', balance), '5': ('登录', login), '6': ('注册', register), '0': ('退出', None) } def main(): while True: print('ATM'.center(22, '-')) for i in func_dic: print('{} {}'.format(i, func_dic[i][0]).center(18)) print('END'.center(22, '-')) cmd = input("请输入编号:").strip() if not cmd.isdigit(): print("必须输入编号") continue if cmd == '0': break if cmd in func_dic: if login_user and cmd == '5': print("请勿重复登录") continue if not login_user and cmd not in ['5', '6']: print("请先登录") else: func_dic[cmd][1]() print(login_user) else: print("编号不存在") if __name__ == '__main__': main()
🐱不负韶华,只争朝夕🍚