今日作业:
1、函数对象优化多分支if的代码练熟
if user_state: list=[0,1,2] dic={ '查询余额请输入','0' '提现请输入','1' '转账请输入','2' } while 1: print(dic) cmd=input('请输入指令:') if cmd in list: call(int(cmd)) break else: print('无效的指令') continue def call(x): if x==0:#查看余额 check_money() elif x==1:#提现 withdraw_deposit() elif x==2:#转账 transfer()
2、编写计数器功能,要求调用一次在原有的基础上加一 温馨提示: I:需要用到的知识点:闭包函数+nonlocal II:核心功能如下: def counter(): x+=1 return x 要求最终效果类似 print(couter()) # 1 print(couter()) # 2 print(couter()) # 3 print(couter()) # 4 print(couter()) # 5
周末作业
编写ATM程序实现下述功能,数据来源于文件db.txt
0、注册功能:用户输入账号名、密码、金额,按照固定的格式存入文件db.txt
1、登录功能:用户名不存在,要求必须先注册,用户名存在&输错三次锁定,登录成功后记录下登录状态(提示:可以使用全局变量来记录)
下述操作,要求登录后才能操作
1、充值功能:用户输入充值钱数,db.txt中该账号钱数完成修改
2、转账功能:用户A向用户B转账1000元,db.txt中完成用户A账号减钱,用户B账号加钱
3、提现功能:用户输入提现金额,db.txt中该账号钱数减少
4、查询余额功能:输入账号查询余额
user_state=0 def login(): list_1=[] tag=1 # while tag: with open(r'C:\Users\Administrator\Desktop\db.txt',mode='rt',encoding='utf-8') as f1: name=input('请输入账户:').split() for line in f1: username=line.split(':')[0] # print(username) userpwd=line.split(':')[1] count=line.split(':')[2] # print(count) money=line.split(':')[3] if name == username: print(name==usernane) pwd=input('请输入密码').strip() if userpwd==pwd: print('登录成功') user_state=name tag=0 break elif userpwd!=pwd: print('密码错误') count+=1 else: ... else: ... else: print('用户不存在请注册') # register() if count == 3: print('用户已锁定') tag = 0 with open (r'C:\Users\Administrator\Desktop\db.txt', mode='rt', encoding='utf-8') as f2: for line in f2: if line.split(':')[0] == user_state: line = f'{username}:{userpwd}:{count}:{money}\n' list4.append(line) with open(r'C:\Users\Administrator\Desktop\db.txt', mode='wt', encoding='utf-8') as f3: f3.write(list4) def register():#注册功能 tag=1 name_exist=0 while tag: with open (r'C:\Users\Administrator\Desktop\db.txt',mode='rt',encoding="utf-8") as f1: username=input('请输入要注册的账户:').strip() for line in f1: name=line.split(':')[0] # print(name) if name==username: print('用户已存在') break tag=0 name_exist=1 else: userpwd=input('请输入要注册的密码:').strip() if name_exist==0: with open(r'C:\Users\Administrator\Desktop\db.txt',mode='wt',encoding='utf-8') as f2: f2.write(f'{username}:{userpwd}:0:0\n') break login() if user_state: list=[0,1,2] dic={ '查询余额请输入','0' '提现请输入','1' '转账请输入','2' } while 1: print(dic) cmd=input('请输入指令:') if cmd in list: call(int(cmd)) break else: print('无效的指令') continue def call(x): if x==0:#查看余额 check_money() elif x==1:#提现 withdraw_deposit() elif x==2:#转账 transfer() def check_money():#查询余额 with open(r'C:\Users\Administrator\Desktop\db.txt',mode='rt',encoding='utf-8') as f1: for line in f1: if line.split(':')[0]==user_state: res=line.strip().split(':')[3] print(res) def withdraw_deposit():#提现 is_withdraw_deposit=0 with open(r'C:\Users\Administrator\Desktop\db.txt', mode='rt', encoding='utf-8') as f1: amount=input('请输入提款金额:') if not amount.isdigit(): print('输入错误') else: for line in f1: if line.split(':')[0] == user_state: money = line.strip().split(':')[3] if int(res)-int(amount)>0: left_money=int(res)-int(amount) is_withdraw_deposit = 1 new_line=line.replace(f'{money}',f'{left_money}') pass else: print('余额不足') if is_withdraw_deposit: list4=[] with open(r'C:\Users\Administrator\Desktop\db.txt',mode='rt',encoding='utf-8') as f2: for line in f2: if line.split(':')[0] == user_state: line=new_line list4.append(line) with open (r'C:\Users\Administrator\Desktop\db.txt',mode='wt',encoding='utf-8') as f3: f3.write(list4) def transfer():#转账 is_transfer = 0 with open(r'C:\Users\Administrator\Desktop\db.txt', mode='rt', encoding='utf-8') as f1: transfer_name=input('请输入要转入的账户:') amount=input('请输入转账金额:') if not amount.isdigit(): print('输入错误') else: for line in f1: if line.split(':')[0]==transfer_name: pass else: print('不支持对外账户哦 0.0') for line in f1: if line.split(':')[0] == user_state: money = line.strip().split(':')[3] if int(res)-int(amount)>0: left_money=int(res)-int(amount) is_transfer = 1 new_line=line.replace(f'{money}',f'{left_money}') pass else: print('余额不足') for line in f1: if line.split(':')[0] == transfer_name: money2 = line.strip().split(':')[3] left_money2 = int(money2)+ int(amount) new_line2=line.replace(f'{money2}',f'{left_money2}') if is_transfer: list5=[] with open(r'C:\Users\Administrator\Desktop\db.txt',mode='rt',encoding='utf-8') as f2: for line in f2: if line.split(':')[0] == user_state: line=new_line elif line.split(':')[0] == transfer_name: line=new_line2 list5.append(line) with open (r'C:\Users\Administrator\Desktop\db.txt',mode='wt',encoding='utf-8') as f3: f3.write(list5)
来源:https://www.cnblogs.com/Franciszw/p/12550098.html