作业 —— day16
今日作业:
1.函数对象优化多分支if的代码练熟
def exit():
print('退出')
# break
def login():
print('登录功能')
def transfer():
print('转账功能')
def check_banlance():
print('查询余额')
def withdraw():
print('提现')
def register():
print('注册')
func_dic = {
'1': login,
'2': transfer,
'3': check_banlance,
'4': withdraw,
'5': register
}
while True:
print("""
0 退出
1 登录
2 转账
3 查询余额
4 提现
5 注册
""")
choice = input('请输入命令编号:').strip()
if not choice.isdigit():
print('必须输入编号,傻叉')
continue
if choice == '0':
break
if choice in func_dic:
func_dic[choice]()
else:
print('输入的指令不存在')
2.编写计数器功能,要求调用一次在原有的基础上加一
def take_num():
x = 0
def counte():
nonlocal x
x+=1
return x
return counte
couter = take_num()
print(couter())
print(couter())
print(couter())
print(couter())
print(couter())
二:周末作业
编写ATM程序实现下述功能,数据来源于文件db.txt
0.注册功能:用户输入账号名、密码、金额,按照固定的格式存入文件db.txt
1.登录功能:用户名不存在,要求必须先注册,用户名存在&输错三次锁定,登录成功后记录下登录状态(提示:可以使用全局变量来记录)
下述操作,要求登录后才能操作
1.充值功能:用户输入充值钱数,db.txt中该账号钱数完成修改
2.转账功能:用户A向用户B转账1000元,db.txt中完成用户A账号减钱,用户B账号加钱
3.提现功能:用户输入提现金额,db.txt中该账号钱数减少
4.查询余额功能:输入账号查询余额
import os
login_user = None
# 注册模块
def register():
inp_usr = input("请输入账号:").strip()
inp_pwd = input("请输入密码:").strip()
inp_money = input("请输入预存金额:").strip()
if not (inp_usr or inp_pwd or inp_money):
print("输入不能为空")
# continue
with open(r'userinfo.txt', mode='at', encoding='UTF-8') as w_register, \
open(r'userinfo.txt', mode='rt', encoding='UTF-8') as r_register, \
open(r'money.txt', mode='at', encoding='UTF-8') as a_money:
res = r_register.read()
if inp_usr not in res:
w_register.write(f'{inp_usr}:{inp_pwd}\n')
a_money.write(f'{inp_usr}:{inp_money}\n')
print('注册成功!')
else:
print('该用户已存在,注册失败!')
# 登录模块
def login():
global login_user
count = 0
while True:
inp_usr = input("请输入账号:").strip()
if os.path.exists(r'locked\{}.txt'.format(inp_usr)):
print("该账号已被锁定")
count = 0
continue
elif login_user == inp_usr:
print('您处于已登录状态,请勿重复登录!')
break
inp_pwd = input("请输入密码:").strip()
with open(r'userinfo.txt', mode='rt', encoding='UTF-8') as r_login:
for line in r_login:
usr, pwd = line.strip().split(':')
if inp_usr == usr and inp_pwd == pwd:
login_user = inp_usr
print('登录成功,请选择')
break
else:
res = r_login.read()
print(res)
print(count)
if inp_usr not in res:
print("该账号不存在")
else:
if count == 2:
print("错误次数过多,账号已被锁定")
with open(r'locked\{}.txt'.format(inp_usr), mode='wt', encoding='UTF-8') as f:
f.write('')
continue
else:
print("密码错误,剩余次数:{}".format(2 - count))
count += 1
break
# 余额查询模块
def balance():
inp_usr = input("请输入要查询余额账号:").strip()
if inp_usr == login_user:
with open(r'money.txt', mode='rt', encoding='UTF-8') as f_balance:
usr, money = f_balance.readline().strip().split(':')
if inp_usr == usr and money:
print('尊敬的用户{},您的余额为{}元'.format(inp_usr, money))
else:
print('请先登录')
login()
# 提现模块
def withdrawal():
inp_usr = input("请输入要提现的账号:").strip()
if inp_usr == login_user:
dic = {}
with open(r'money.txt', mode='rt', encoding='UTF-8') as r_withdrawal:
for line in r_withdrawal:
usr, money = line.strip().split(':')
dic[usr] = int(money)
while True:
money = input('请输入要提现的金额:').strip()
if not money.isdigit():
print('必须输入数字!')
continue
money = int(money)
dic[inp_usr] -= money
with open(r'money.txt', mode='wt', encoding='UTF-8') as w_withdrawal:
for usr, money in dic.items():
w_withdrawal.write(f'{usr}:{money}\n')
with open(r'money.txt', mode='rt', encoding='UTF-8') as f_balance:
usr, money = f_balance.readline().strip().split(':')
if inp_usr == usr and money:
print('提现成功,您的余额为{}元'.format(money))
break
else:
print('请先登录')
login()
# 提现模块
def recharge():
inp_usr = input("请输入要充值的账号:").strip()
if inp_usr == login_user:
dic = {}
with open(r'money.txt', mode='rt', encoding='UTF-8') as r_recharge:
for line in r_recharge:
usr, money = line.strip().split(':')
dic[usr] = int(money)
while True:
money = input('请输入要提现的金额:').strip()
if not money.isdigit():
print('必须输入数字!')
continue
money = int(money)
dic[inp_usr] += money
with open(r'money.txt', mode='wt', encoding='UTF-8') as w_withdrawal:
for usr, money in dic.items():
w_withdrawal.write(f'{usr}:{money}\n')
with open(r'money.txt', mode='rt', encoding='UTF-8') as f_balance:
usr, money = f_balance.readline().strip().split(':')
if inp_usr == usr and money:
print('充值成功,您的余额为{}元'.format(money))
break
else:
print('请先登录')
login()
# 转账模块
def transfer():
inp_usr = input("请输入您的账号:").strip()
if inp_usr == login_user:
inp_usr2 = input("请输入对方账号:").strip()
t_money = input("请输入转账金额:").strip()
if not t_money.isdigit():
print('必须输入数字!')
else:
t_money = int(t_money)
dic = {}
with open(r'money.txt', mode='rt', encoding='UTF-8') as f_balance:
for line in f_balance:
usr, money = line.strip().split(':')
dic[usr] = int(money)
if inp_usr2 not in dic:
print('对方账户不存在,请重新输入')
return
print('转账前:', dic[inp_usr])
if dic.get(inp_usr) >= t_money:
dic[inp_usr] -= t_money
dic[inp_usr2] -= t_money
print('转账后:', dic[inp_usr])
with open(r'money.txt', mode='wt', encoding='UTF-8') as f:
for usr, money in dic.items():
f.write(f'{usr}:{money}\n')
else:
print('请先登录')
login()
func_dic = {
'1': login,
'2': transfer,
'3': balance,
'4': withdrawal,
'5': register
}
while True:
print("""
0 退出
1 登录
2 转账
3 查询余额
4 提现
5 注册
""")
choice = input('请输入命令编号:').strip()
if not choice.isdigit():
print('必须输入编号,傻叉')
continue
if choice == '0':
break
if choice in func_dic:
func_dic[choice]()
else:
print('输入的指令不存在')