作业20200320
1、函数对象优化多分支if的代码练熟
def login():
print('this is login')
def logout():
print('this is logout')
def register():
print('this is register')
cdm_dict = {
'0': ('退出', logout),
'1': ('登录', login),
'2': ('注册', register),
}
def atm():
print('欢迎使用笨笨ATM')
while True:
for k, v in cdm_dict.items():
print(k, v[0])
cmd = input('小主人请选一个编号吧>>>:').strip()
if not cmd.isdigit() or cmd not in cdm_dict:
print('小主人,别闹')
continue
cdm_dict.get(cmd)[1]()
if __name__ == '__main__':
atm()
2、编写计数器功能,要求调用一次在原有的基础上加一
def outer():
x = 0
def counter():
nonlocal x
x += 1
return x
return counter
counter = outer()
print(counter()) # 1
print(counter()) # 2
print(counter()) # 3
print(counter()) # 4
周末作业
编写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 get_accounts():
# 从硬盘文件将账户信息读入内存
all_accounts = {}
with open('db.txt', 'rt', encoding='utf8') as f:
for line in f:
if not line: continue # 如果出现多余的空行,就next
name, pawd, counts, balance = line.strip().split(':')
# 将名字做字典的key,密码、登录次数、余额做value
all_accounts[name] = [pawd, int(counts), int(balance)]
return all_accounts
def update_db(all_accounts):
# 更新用户信息到硬盘文件
with open('db.txt.swap', 'wt', encoding='utf8') as f:
for k, v in all_accounts.items():
info = f'{k}:{":".join(map(str, v))}\n'
f.write(info)
os.remove('db.txt')
os.rename('db.txt.swap', 'db.txt')
def append2file(new_user):
with open('db.txt', 'at', encoding='utf8') as f:
f.write(f'{":".join(new_user)}:0:0\n')
def check_login(func):
def inner():
if not current_user: print('请先登录')
else: func()
return inner
def login():
global current_user
if current_user:
print('您已经登录,无需再登录')
return
tag = True
while tag:
print('欢迎光临,请登录')
name = input('用户名 >>>:').strip()
pawd = input('密码 >>>:').strip()
if name not in all_accounts:
while tag:
choice = input(f'用户名:{name} 不存在, 登录(L)\注册(R):').strip().lower()
if choice == 'l':
break
elif choice == 'r':
register()
break
else:
print('好好输入行不')
continue
if all_accounts[name][1] == 3:
print('不好意思该账号已被冻结,详情请咨询客服')
continue
if pawd == all_accounts[name][0]:
print(f'登录成功,欢迎{name}')
all_accounts[name][1] = 0
# current_user = ['egon', '1234', 0, 1000]
current_user = [name, pawd, all_accounts[name][1], all_accounts[name][2]]
update_db(all_accounts)
break
# 下面是密码错误的情况
all_accounts[name][1] += 1
s = f'密码错误,您还有{3-all_accounts[name][1]}次登录机会'
if all_accounts[name][1] == 3:
s = f'登录失败3次,您的账号被冻结,请联系客服解冻'
tag = False
print(s)
# 将登录失败信息保存到硬盘
update_db(all_accounts)
def logout():
print('欢迎下次光临', end='')
input('按任意键退出:')
exit()
def register():
global all_accounts
while True:
name = input('用户名 >>>:').strip()
pawd = input('密码 >>>:').strip()
re_pawd = input('确认密码 >>>:').strip()
if name in all_accounts:
print('该用户名已存在,请换一个')
continue
if pawd != re_pawd:
print('两次输入的密码不一致,请重新输入')
continue
# 下面是可以注册的情况
new_user = name, pawd
append2file(new_user)
all_accounts = get_accounts()
print('注册成功,请登录')
break
@check_login
def recharge():
if not current_user:
print('请先登录好吧')
return
while 1:
add_balance = input('请输入充值金额(元)>>>:').strip()
if not add_balance.isdigit():
print('请确保充值金额为整数')
continue
current_user[-1] += int(add_balance)
name = current_user[0]
all_accounts[name][-1] += int(add_balance)
update_db(all_accounts)
print(f'本次充值:{add_balance}元,账户余额:{current_user[-1]}')
break
@check_login
def transfer():
tag = True
while tag:
other = input('请指定对方账号>>>:').strip()
if other not in all_accounts:
print(f'账号:{other} 不存在,请核对')
continue
money = input('请输入转账金额 >>>:').strip()
if not money.isdigit():
print('请输入整数')
continue
money = int(money)
if money >= current_user[-1]:
print('不好意思,您账户余额不足无法转账')
break
# 下面是可以转账的情况
while tag:
is_sure = input(f'您确定给 {other},转账:¥{money}元(y/n)').strip().lower()
if is_sure == 'y':
current_user[-1] -= money
all_accounts[current_user[0]][-1] -= money
all_accounts[other][-1] += money
update_db(all_accounts)
print('转账成功')
tag = False
elif is_sure == 'n':
print('取消本次转账交易')
tag = False
@check_login
def withdraw():
while 1:
money = input('请输入提现金额(元)>>>:').strip()
if not money.isdigit():
print('请确保提现金额为整数')
continue
if int(money) >= current_user[-1]:
print('账户余额不足,无法提现')
continue
current_user[-1] -= int(money)
name = current_user[0]
all_accounts[name][-1] -= int(money)
update_db(all_accounts)
print(f'本次提现:{money}元,账户余额:{current_user[-1]}')
break
@check_login
def balance():
my_balance = current_user[-1]
print(f'{current_user[0]},您当前账户余额:¥{my_balance}元')
# 功能集
cmd_dict = {
'1': ('退出', logout),
'2': ('登录', login),
'3': ('注册', register),
'4': ('查询', balance),
'5': ('转账', transfer),
'6': ('充值', recharge),
'7': ('提现', withdraw),
}
# 记录当前用户信息
current_user = {}
# 保存所有用户信息
all_accounts = get_accounts()
def atm():
print('欢迎使用笨笨ATM'.center(20, '='))
while 1:
for k, v in cmd_dict.items():
print(k, v[0], sep='\t')
cmd = input('小主人,请选一个编号吧:').strip()
if not cmd.isdigit() or cmd not in cmd_dict:
print('小主人,别闹')
continue
# 调用功能
func = cmd_dict.get(cmd)[1]
func()
if __name__ == '__main__':
atm()