def exit():
print('退出')
def login():
print('登录功能')
def transfer():
print('转账功能个')
def check_balance():
print('余额查询功能')
def withdraw():
print('提现功能')
def register():
print('注册功能')
func_dic = {
'1': ('login',login),
'2': ('transfer',transfer),
'3': ('check_balance',check_balance),
'4': ('withdraw',withdraw),
'5': ('register',register),
'0': ('exit',exit),
}
def main():
for i, j in func_dic.items():
print('{}:{}'.format(i, j[0]))
while True:
operate = input('请输入待选事项序号:')
if not operate.isdigit():
print('请重新输入序号')
continue
if operate in func_dic:
func_dic[operate][1]()
else:
print('请重新输入正确序号')
main()
def outter_calculate():
n = 0
def calculate():
nonlocal n
n += 1
return n
return calculate
calculate = outter_calculate()
print(calculate())
print(calculate())
print(calculate())
print(calculate())
print(calculate())
'''
ATM程序
# 编写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():
while True:
account_input = input('请输入注册账号:').strip()
password_input = input('请输入密码:').strip()
money_input = input('请输入存款额:').strip()
with open(r'db.txt', 'rt', encoding='utf-8') as f1,
open(r'db.txt', 'at', encoding='utf-8') as f2:
for line in f1:
account, password, money = line.strip().split(':')
if account_input == account:
print('账号已存在,请重新输入!')
break
else:
f2.write('
{}:{}:{}'.format(account_input, password_input, money_input))
global login_user
login_user = account_input
print('账号注册成功')
break
# 登录功能
# login_user = None
def login():
amount = True
n = 0
while amount:
account_input = input('请输入账号:').strip()
password_input = input('请输入密码:').strip()
with open(r'db.txt', 'rt', encoding='utf-8') as f1,
open(r'blacklist.txt', 'rt', encoding='utf-8') as f2:
for line1 in f2:
account_black, *_ = line1.strip().split(':')
if account_black == account_input:
print('对不起,该账号已被锁定,程序即将退出!')
exit()
else:
for line2 in f1:
account, password, *_ = line2.strip().split(':')
if account_input == account:
if password_input == password:
global login_user
login_user = account_input
# print('登陆成功')
print(login_user)
amount = False
break
elif n == 2:
with open(r'blacklist.txt', 'at', encoding='utf-8') as f3:
f3.write('{}:{}
'.format(account_input, password_input))
amount = False
print('非常抱歉,您已输错三次密码')
print('您的账号已被锁定')
break
elif password_input != password:
n += 1
print('密码错误,请重新输入!')
print(n)
break
else:
print('用户不存在,请先注册')
register()
amount = False
# 登录后操作
# 充值功能
def recharge():
account = login_user
money_input = input('请输入充值金额:').strip()
money_input = int(money_input)
with open(r'db.txt', 'rt', encoding='utf-8') as f1,
open(r'db.txt.swap', 'wt', encoding='utf-8') as f2:
for line in f1:
acc, *_, money = line.strip().split(':')
money = int(money)
if acc == account:
line = line.replace(str(money), str(money + money_input))
f2.write(line)
print('充值金额为{},余额为{}'.format(money_input, money + money_input))
else:
f2.write(line)
os.remove(r'db.txt')
os.rename(r'db.txt.swap', r'db.txt')
# 转账功能
def transfer():
account = login_user
account_receive = input('请输入接收账户:')
money_input = input('请输入转账金额:')
money_input = int(money_input)
with open(r'db.txt', 'rt', encoding='utf-8') as f1,
open(r'db.txt.swap', 'wt', encoding='utf-8') as f2:
for line in f1:
acc, *_, money = line.strip().split(':')
money = int(money)
if acc == account:
line = line.replace(str(money), str(money - money_input))
f2.write(line)
print('{}向{}转账{}元,{}余额为{}'.format(account, account_receive, money_input, account, money - money_input))
elif acc == account_receive:
line = line.replace(str(money), str(money + money_input))
f2.write(line)
print('{}余额为{}'.format(account_receive, money + money_input))
else:
f2.write(line)
os.remove(r'db.txt')
os.rename(r'db.txt.swap', r'db.txt')
# 提现功能
def withdraw():
account = login_user
money_input = input('请输入提现金额:')
money_input = int(money_input)
with open(r'db.txt', 'rt', encoding='utf-8') as f1,
open(r'db.txt.swap', 'wt', encoding='utf-8') as f2:
for line in f1:
acc, *_, money = line.strip().split(':')
money = int(money)
if acc == account:
line = line.replace(str(money), str(money - money_input))
print(money - money_input)
f2.write(line)
print('提现金额为{},余额为{}'.format(money_input, money - money_input))
else:
f2.write(line)
os.remove(r'db.txt')
os.rename(r'db.txt.swap', r'db.txt')
# 余额查询
def remainder():
account = login_user
with open(r'db.txt', 'rt', encoding='utf-8') as f1:
for line in f1:
acc, *_, money = line.strip().split(':')
if acc == account:
print('余额为{}元'.format(money))
# 主程序
dic_outer = {
'1': ('注册', register),
'2': ('登录', login),
'0': ('退出', None)
}
dic_inner = {
'1': ('充值', recharge),
'2': ('转账', transfer),
'3': ('提现', withdraw),
'4': ('查询余额', remainder),
'0': ('退出', None)
}
while True:
for i, j in dic_outer.items():
print('{}:{}'.format(i, j[0]))
choice_out = input('请选择操作项目').strip()
if not choice_out.isdigit():
print('请重新输入正确数字')
if choice_out in dic_outer:
if choice_out == '0':
exit()
else:
dic_outer[choice_out][1]()
if login_user == None:
print('程序即将退出')
break
# print(login_user)
while True:
for i, j in dic_inner.items():
# print(login_user)
print('{}:{}'.format(i, j[0]))
choice_in = input('请选择操作项目').strip()
if not choice_in.isdigit():
print('请重新输入正确数字')
if choice_in in dic_inner:
if choice_in == '0':
exit()
else:
dic_inner[choice_in][1]()
else:
print('数字超限,请重新输入')