1、函数对象优化多分支if的代码练熟
//代码
def login(): print('登录功能') def register(): print('注册') func_dic={ "0":["退出",None], '1':["登录",login], '2':["注册",register] } while True: for i in func_dic: print(i,func_dic[i][0]) choice = input('请输入命令编号:').strip() if not choice.isdigit(): print('必须输入编号,傻叉') continue if choice == '0': break if choice in func_dic: func_dic[choice]() else: print('输入的指令不存在')
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
#代码
def wrapeer(): x = 0 def couter(): nonlocal x x += 1 return x return couter couter = wrapeer() 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、查询余额功能:输入账号查询余额
文件目录:
db.txt文件内容
zhangsan:123:200 lisi:123:10 egon:123:455365 alex:123:152222 jc:123:0 lxx:123:13000
lock_db.txt文件内容(用来记录用户锁定信息)
zhangsan:0
lisi:0
egon:0
alex:0
jc:0
lxx:0
atm程序v3.py代码:
# 开发人员: alias # 开发时间: 2020/3/20 7:29 # 文件名称: atm程序v3.py # 开发工具: PyCharm import os,time from prettytable import PrettyTable login_user=None def lock_user(username): """记录锁定用户信息:打开一次,用户的锁定记录+1""" with open("lock_db.txt", "r", encoding="utf-8") as f, open(".lock_db.txt.swap", "w", encoding="utf-8") as fs: for line in f: name,state=line.strip(" ").split(":") state=int(state) if username in line: state+=1 fs.write("{}:{} ".format(name,state)) os.remove("lock_db.txt") os.rename(".lock_db.txt.swap", "lock_db.txt") def check_lock_count(username): """用于检测锁定标记次数""" with open("lock_db.txt", "r", encoding="utf-8") as f: for line in f: name,state=line.strip(" ").split(":") if state == "3": print("输错3次,用户已锁定,请5分钟后再试。") time.sleep(10) return 1 def unlock_user(username): """用于执行完锁定,解锁用户""" with open("lock_db.txt", "r", encoding="utf-8") as f, open(".lock_db.txt.swap", "w", encoding="utf-8") as fs: for line in f: name,state=line.strip(" ").split(":") if username in line: state=0 fs.write("{}:{} ".format(name, state)) os.remove("lock_db.txt") os.rename(".lock_db.txt.swap", "lock_db.txt") def user_login(): """用户登录验证""" global login_user #申明函数中是全局的login_user user_exist=False #标记登录用户是否存在 flag=True inp_name = input("请输入用户名:>>").strip() #检测用户是否存在 with open("db.txt", "r", encoding="utf-8") as f: for line in f: if inp_name in line: user_exist=True break if user_exist: while flag: inp_pwd = input("请输入密码:>>").strip() with open("db.txt", "r", encoding="utf-8") as f: for line in f: name, pwd, money = line.strip(" ").split(":") if inp_name == name and inp_pwd == pwd: print("用户{}登录成功。".format(inp_name)) login_user = inp_name flag=False break else: print("密码错误,请重新输入。") lock_user(inp_name) res=check_lock_count(inp_name) if res == 1: unlock_user(inp_name) user_login() else: print("用户{}不存在,请先注册后再登录。") user_register() def user_register(): """注册用户""" add_name = input("请输入注册的用户名:>>").strip() add_pwd = input("请输入注册用户的密码:>>").strip() with open("db.txt", "r", encoding="utf-8") as f: for line in f: if add_name in line: print("用户{}已被注册".format(add_name)) return with open("db.txt", "a", encoding="utf-8") as fa: fa.write("{}:{}:{} ".format(add_name, add_pwd, "0")) print("用户{}注册成功。".format(add_name)) def deposit(): """用户充值""" deposit_money = input("请输入充值金额:>>>").strip() deposit_money = int(deposit_money) with open("db.txt", "r", encoding="utf-8") as f, open(".db.txt.swap", "w", encoding="utf-8") as fs: for line in f: name, pwd, mon = line.strip(" ").split(":") mon = int(mon) if login_user in line: mon += deposit_money fs.write("{}:{}:{} ".format(name, pwd, mon)) else: print("用户{}成功充值{}元。".format(login_user, deposit_money)) os.remove("db.txt") os.rename(".db.txt.swap", "db.txt") def transfer(): """用户转账""" user_exist = False money_flag=True transfer_in_user=input("请输入转账账户:>>>").strip() transfer_money=int(input("请输入转账金额:>>>").strip()) with open("db.txt", "r", encoding="utf-8") as f: for line in f: if transfer_in_user in line: user_exist = True break with open("db.txt", "r", encoding="utf-8") as f: for line in f: name, pwd, mon = line.strip(" ").split(":") mon = int(mon) if login_user in line: if mon < transfer_money: money_flag=False if user_exist: if money_flag: with open("db.txt", "r", encoding="utf-8") as f, open(".db.txt.swap", "w", encoding="utf-8") as fs: for line in f: name, pwd, mon = line.strip(" ").split(":") mon = int(mon) if login_user in line: mon -= transfer_money if transfer_in_user in line: mon += transfer_money fs.write("{}:{}:{} ".format(name, pwd, mon)) else: print("用户{}成功转账{}元到{}用户。".format(login_user, transfer_money, transfer_in_user)) os.remove("db.txt") os.rename(".db.txt.swap", "db.txt") else: print("尊敬的用户{}您的余额不足,无法进行转账操作。".format(login_user)) else: print("{}用户不存在".format(transfer_in_user)) def withdraw(): """用户提现""" money_flag = True withdraw_money=int(input("请输入提现金额:>>>").strip()) with open("db.txt", "r", encoding="utf-8") as f: for line in f: name, pwd, mon = line.strip(" ").split(":") mon = int(mon) if login_user in line: if mon < withdraw_money: money_flag=False if money_flag: with open("db.txt", "r", encoding="utf-8") as f, open(".db.txt.swap", "w", encoding="utf-8") as fs: for line in f: name, pwd, mon = line.strip(" ").split(":") mon = int(mon) if login_user in line: mon -= withdraw_money fs.write("{}:{}:{} ".format(name, pwd, mon)) else: print("用户{}成功提现{}元。".format(login_user, withdraw_money)) os.remove("db.txt") os.rename(".db.txt.swap", "db.txt") else: print("账户余额不足。无法进行提现操作。") def show_money(): """显示余额""" with open("db.txt", "r", encoding="utf-8") as f: for line in f: name, pwd, mon = line.strip(" ").split(":") if login_user in line: print("{}用户账户余额为{}".format(login_user, mon)) def logout(): """ 退出登录 :return: """ u_logout=input("确认退出Y/N,y/s>>>").strip() if u_logout == "Y" or u_logout == "y": exit() dic_login_menu={ "0":("退出",logout), "1":("登录",user_login), "2":("注册",user_register), } def func_menu_login(): """打印登录之前功能菜单""" x = PrettyTable(field_names=["功能id", "功能"]) for i in dic_login_menu: x.add_row([i,dic_login_menu[i][0]]) print(x) dic_menu_use={ "0":("充值",deposit), "1":("转账",transfer), "2":("提现",withdraw), "3":("查询余额",show_money), "4":("返回上一层",None), "5":("退出",logout), } def func_menu_use(): """打印登录成功的功能菜单""" x = PrettyTable(field_names=["功能id", "功能"]) for i in dic_menu_use: x.add_row([i,dic_menu_use[i][0]]) print(x) flag = True while flag: func_menu_login() func_id = input("请输入功能id:>>").strip() if func_id in dic_login_menu: dic_login_menu[func_id][1]() if login_user: while flag: func_menu_use() func_id_use = input("请输入功能id:>>").strip() if func_id_use == "4": break if func_id_use in dic_menu_use: dic_menu_use[func_id_use][1]() else: print("请正确输入功能id.") else: print("请正确输入功能id.")