day16 练习
# 编写ATM程序 # a)登录功能(3 # 分) def login(): dic={} with open(r'D:\cool\user',mode='rt',encoding='utf-8')as x: for y in x: name,pwd,money=y.strip().split(':') dic[name]=[pwd,money] print(dic) inp_user=input('请输入用户名:') inp_pwd=input('请输入密码:') if inp_user in dic: if inp_pwd == dic[inp_user][0]: print('登录成功') global user_info user_info = {inp_user:[dic[inp_user][0],dic[inp_user][1]]} return True else: print('登录失败,请重新登录') return True else: print('用户名不存在,请注册') return True # b)注册功能(3 # 分) def registered(): dic = {} with open(r'D:\cool\user',mode='rt',encoding='utf-8')as x: for y in x: name,pwd,money=y.strip().split(':') dic[name]=[pwd,money] inp_name=input('请输入要注册的用户名:') inp_pwd=input('请输入注册密码:') inp_cpwd=input('请再次确认注册密码:') if inp_name in dic: print("该用户名已被注册") return True else: if inp_pwd==inp_cpwd: print('注册成功') with open(r'D:\cool\user',mode='at',encoding='utf-8')as x: x.write(f'{inp_name}:{inp_pwd}:0\n') return True else: print('注册密码输入错误') return True # registered() # 注意: 以下功能,若没有登录则不能使用。 # c)查看余额功能(3分) def view_balance(): for k,v in user_info.items(): pass if user_info == {}: print('请登录') return True else: print(f'您的余额为{v[1]}元') return False # view_balance() # d)提现功能(3分) def withdraw(): for k,v in user_info.items(): pass v[1]=int(v[1]) if user_info == {}: print('请登录') return True else: amount=input('请输入要提现的金额:') amount=int(amount) dic = {} with open(r'D:\cool\user', mode='rt', encoding='utf-8')as x: for y in x: name, pwd, money = y.strip().split(':') dic[name] = [pwd, money] balance=v[1]-amount balance=str(balance) if amount>v[1]: print('余额不足') return True else: print(f'提现金额为{amount} 余额为{balance}') dic[k][1]=balance dic=str(dic) n=dic.strip("'[]}{").replace("': ['",':').replace("', '",':').replace("'], '",'\n') with open(r'D:\cool\user',mode='wt',encoding='utf-8')as i: i.write(f'{n}\n') print('提现成功') return False # e)转账功能(3# 分) def transfer(): for k,v in user_info.items(): pass v[1]=int(v[1]) if user_info == {}: print('请登录') return True else: t_user=input('请输入您要转账的用户:') t_money=input('请输入要转账的金额:') t_money=int(t_money) print(f'你要转账的用户是{t_user} 金额为{t_money}元') dic = {} with open(r'D:\cool\user', mode='rt', encoding='utf-8')as x: for y in x: name, pwd, money = y.strip().split(':') dic[name] = [pwd, money] if t_user in dic : if t_user == k: print('转账对象不可以是本用户') return True if t_money > v[1]: print('您输入的金额已超出您的余额 请重新输入:') return True else: # print(dic[t_user][1]) # print(v[1]) c = int(dic[t_user][1]) d= int(v[1]) a=d-t_money b=c+t_money dic[k][1]=a dic[t_user][1]=b dic = str(dic) n = dic.strip("'[]}{").replace("': ['", ':').replace("', '", ':').replace("'], '", '\n').replace("', ",':').replace("], '","\n") with open(r'D:\cool\user', mode='wt', encoding='utf-8')as i: i.write(f'{n}\n') print('转账成功') return False else: print('转账目标客户不存在') return True user_info={} user_func={ 'a':login, 'b':registered, 'c':view_balance, 'd':withdraw, 'e':transfer } tag=True while tag: func=input('=========ATM==========\n' '登录 [a] 注册 [b]\n' '提现 [d] 转账 [e]\n' '余额查询 [c]\n' '=========END==========\n' '请输入你要办理的业务:') if func in user_func: tag=user_func[func]() else: print('请输入正确命令')