编写ATM程序
实现下述功能:
1、充值功能:用户输入充值钱数,db.txt中该账号钱数完成修改
2、转账功能:用户A向用户B转账1000元,db.txt中完成用户A账号减钱,用户B账号加钱
3、提现功能:用户输入提现金额,db.txt中该账号钱数减少
4、查询余额功能:输入账号查询余额
5、数据来源于文件db.txt(所有的增删改查都在同一文件当中)
选做中的选做:登录功能
用户登录成功后,内存中记录下该状态,上述功能以当前登录状态为准,必须先登录才能操作
代码如下:
login_name = None #选做中的选做重点
# 操作系统相关操作功能
def move_fuc():
import os
os.remove(r'{}'.format('db.txt'))
os.rename(r'{}.swap'.format('db.txt'), r'{}'.format('db.txt'))
# 登录功能
def login_func():
name_user = input('请输入您的账号:').strip()
name_pwd = input('请输入您的密码:').strip()
global login_name
if not login_name:
with open('db.txt','r',encoding='utf-8') as f:
for line in f:
x, y, *_ = line.strip().split(':')
if x == name_user and y == name_pwd:
print('登陆成功')
login_name = name_user
return login_name
else:
print('账号密码错误')
else:
print('您已经登录了,请勿重复登录')
# 充值功能
def recharge_fuc():
global login_name
while True:
if login_name:
money = input('输入您想充值的金额:')
with open(r'db.txt', 'rb') as rf,
open(r'db.txt.swap', 'wb') as wf:
while True:
user_msg = rf.readline().decode('utf-8')
*_, z = user_msg.strip().split(':')
if login_name in user_msg:
money = int(z) + int(money)
data = user_msg.replace('{}'.format(z), '{}'.format(money))
wf.write(data.encode('utf-8'))
else:
wf.write('{}'.format(user_msg).encode('utf-8'))
if len(user_msg) == 0:
break
move_fuc()
print('充值成功,现有金额为{}元'.format(money))
return '充值成功'
else:
print('还没有登录,请登录后再操作,输入P登录或者输入任意键退出')
inp_N = input('请输入符号:>>')
if inp_N == 'P':
login_func()
else:
print('退出')
break
# 转账功能
def take_fuc():
global login_name
tag = 0
while True:
if login_name:
name = input('输入您想转账的人:')
with open(r'db.txt', 'rb') as f,
open(r'db.txt.swap', 'wb') as wf:
while True:
take_msg = f.readline().decode('utf-8')
x, *_= take_msg.strip().split(':')
if name == x:
tag = 1
break
elif not len(take_msg):
print('本银行没有该账号,玩呢?')
return
f.seek(0, 0)
money = input('输入您想转账的金额:')
while tag:
take_msg = f.readline().decode('utf-8')
*_, z = take_msg.strip().split(':')
if name in take_msg:
money1 = int(z) + int(money)
data = take_msg.replace('{}'.format(z), '{}'.format(money1))
wf.write(data.encode('utf-8'))
elif login_name in take_msg:
if int(z) > int(money):
money2 = int(z) - int(money)
data = take_msg.replace('{}'.format(z), '{}'.format(money2))
wf.write(data.encode('utf-8'))
continue
else:
print('转账现金已经超过余额')
return '转账现金已经超过余额'
else:
wf.write('{}'.format(take_msg).encode('utf-8'))
if len(take_msg) == 0:
print('转账成功')
query_fuc()
break
move_fuc()
break
else:
print('还没有登录,请登录后再操作,输入P登录或者输入任意键退出')
inp_N = input('请输入符号:>>')
if inp_N == 'P':
login_func()
else:
print('退出')
break
# 查询余额功能
def query_fuc():
global login_name
while True:
if login_name:
with open(r'db.txt','rb') as f:
while True:
query_msg = f.readline().decode('utf-8')
if login_name in query_msg:
*_ , z = query_msg.strip().split(':')
print('{}的账号余额为{}元'.format(login_name,z))
return z
else:
print('还没有登录,请登录后再操作,输入P登录或者输入任意键退出')
inp_N = input('请输入符号:>>')
if inp_N == 'P':
login_func()
else:
print('退出')
break
# 提现功能
def cash_func():
global login_name
while True:
if login_name:
money_cash = input('请输入您的提现金额:>>').strip()
with open(r'db.txt', 'rb') as f,
open(r'db.txt.swap', 'wb') as wf:
while True:
cash_msg = f.readline().decode('utf-8')
if login_name in cash_msg:
*_ , z = cash_msg.strip().split(':')
if int(z) > int(money_cash):
money1 = int(z) - int(money_cash)
data = cash_msg.replace('{}'.format(z), '{}'.format(money1))
wf.write(data.encode('utf-8'))
else:
print('余额不足,提现失败')
return '余额不足,提现失败'
else:
wf.write('{}'.format(cash_msg).encode('utf-8'))
if len(cash_msg) == 0:
break
move_fuc()
print('提现成功')
query_fuc()
return '提现成功'
else:
print('还没有登录,请登录后再操作,输入P登录或者输入任意键退出')
inp_N = input('请输入符号:>>')
if inp_N == 'P':
login_func()
else:
print('退出')
break
# 主函数体
def main_func():
contests_atm = {
'1':login_func,
'2':recharge_fuc,
'3':take_fuc,
'4':cash_func,
'5':query_fuc,
'6':'6'
}
while True:
print("""
==============ATM====================
'1':登录
'2':充值
'3':转账
'4':提现
'5':余额
'6':退出
==============END====================
""")
cmd = input('请输入命令编号>>: ').strip()
if not cmd.isdigit():
print('必须输入命令编号的数字,傻叉')
continue
if cmd not in contests_atm:
print('输入的命令不存在')
continue
if cmd == '6':
print('已成功退出!')
break
contests_atm[cmd]()
main_func()
问题:
1、while循环的时候解压赋值只能取一个,去两个及以上就会报错,for循环反而没问题,在解压赋值之前就要判断,while循环需要考虑的重中之重。
2、当定义了字典函数以后,value后面不能加(),否则会直接运行,不知道为什么。(已解决,写的时候知识点没有讲,自己摸索的)
3、反省点,在b模式下进行文件读写操作时,要时刻注意指针的位置!
更新
1、注册功能:用户输入账号名、密码、金额,按照固定的格式存入文件db.txt(自己同时把黑名单系统也加入其中,只要三次错误谁都救不了!账户锁死)
2、登录功能:用户名不存在,要求必须先注册,用户名存在&输错三次锁定,登录成功后记录下登录状态(提示:可以使用全局变量来记录)
login_name = None #选做中的选做重点
# 操作系统相关操作功能
def move_fuc():
import os
os.remove(r'{}'.format('db.txt'))
os.rename(r'{}.swap'.format('db.txt'), r'{}'.format('db.txt'))
# 注册功能
def register_func():
locked = '1'
name_user = input('请输入您的注册账号:>>').strip()
name_pwd = input('请输入您的注册密码:>>').strip()
name_money = input('请输入您的初始金钱:>>').strip()
with open(r'db.txt', 'rb+') as f:
while True:
res = f.readline().decode('utf-8')
if not len(res):
break
p,x, *_ = res.strip().split(':')
if x == name_user:
print('该账号已被注册,请重新注册')
return
f.write('
{}:{}:{}:{}'.format(locked,name_user, name_pwd, name_money).encode('utf-8'))
print('注册成功')
def login_fuc():
global login_name
if not login_name:
name_user = input('请输入您的账号:>>').strip()
i = 0
tag = 1
with open(r'db.txt', 'rb') as rf,
open(r'db.txt.swap', 'wb') as wf:
while tag:
log_res = rf.readline().decode('utf-8')
if not len(log_res):
print('找不到该账号,请注册')
return
p,x,y,z = log_res.strip().split(':')
if x == name_user:
tag = 0
if p == '0':
print('账号已被锁定,告辞!')
return
while i <3:
name_pwd = input('请输入您的密码')
if name_pwd == y:
print('成功登陆')
login_name = name_user
return
else:
i += 1
print('账号密码输入错误')
else:
print('连续输入三次,账号锁定')
rf.seek(0,0)
while True:
locked_res = rf.readline().decode('utf-8')
if len(locked_res) == 0:
break
a,*_ = locked_res.strip().split(':')
if x in locked_res:
data = locked_res.replace('{}'.format(a), '0')
wf.write(data.encode('utf-8'))
else:
wf.write('{}'.format(locked_res).encode('utf-8'))
move_fuc()
else:print('账号不能重复登录')
# 充值功能
def recharge_fuc():
global login_name
while True:
if login_name:
money = input('输入您想充值的金额:')
with open(r'db.txt', 'rb') as rf,
open(r'db.txt.swap', 'wb') as wf:
while True:
user_msg = rf.readline().decode('utf-8')
*_, z = user_msg.strip().split(':')
if login_name in user_msg:
money = int(z) + int(money)
data = user_msg.replace('{}'.format(z), '{}'.format(money))
wf.write(data.encode('utf-8'))
else:
wf.write('{}'.format(user_msg).encode('utf-8'))
if len(user_msg) == 0:
break
move_fuc()
print('充值成功,现有金额为{}元'.format(money))
return '充值成功'
else:
print('还没有登录,请登录后再操作,输入P登录或者输入任意键退出')
inp_N = input('请输入符号:>>')
if inp_N == 'P':
login_func()
else:
print('退出')
break
# 转账功能
def take_fuc():
global login_name
tag = 0
while True:
if login_name:
name = input('输入您想转账的人:')
with open(r'db.txt', 'rb') as f,
open(r'db.txt.swap', 'wb') as wf:
while True:
take_msg = f.readline().decode('utf-8')
p,x, *_= take_msg.strip().split(':')
if name == x:
tag = 1
break
elif not len(take_msg):
print('本银行没有该账号,玩呢?')
return
f.seek(0, 0)
money = input('输入您想转账的金额:')
while tag:
take_msg = f.readline().decode('utf-8')
*_, z = take_msg.strip().split(':')
if name in take_msg:
money1 = int(z) + int(money)
data = take_msg.replace('{}'.format(z), '{}'.format(money1))
wf.write(data.encode('utf-8'))
elif login_name in take_msg:
if int(z) > int(money):
money2 = int(z) - int(money)
data = take_msg.replace('{}'.format(z), '{}'.format(money2))
wf.write(data.encode('utf-8'))
continue
else:
print('转账现金已经超过余额')
return '转账现金已经超过余额'
else:
wf.write('{}'.format(take_msg).encode('utf-8'))
if len(take_msg) == 0:
print('转账成功')
query_fuc()
break
move_fuc()
break
else:
print('还没有登录,请登录后再操作,输入P登录或者输入任意键退出')
inp_N = input('请输入符号:>>')
if inp_N == 'P':
login_func()
else:
print('退出')
break
# 查询余额功能
def query_fuc():
global login_name
while True:
if login_name:
with open(r'db.txt','rb') as f:
while True:
query_msg = f.readline().decode('utf-8')
if login_name in query_msg:
*_ , z = query_msg.strip().split(':')
print('{}的账号余额为{}元'.format(login_name,z))
return z
else:
print('还没有登录,请登录后再操作,输入P登录或者输入任意键退出')
inp_N = input('请输入符号:>>')
if inp_N == 'P':
login_func()
else:
print('退出')
break
# 提现功能
def cash_func():
global login_name
while True:
if login_name:
money_cash = input('请输入您的提现金额:>>').strip()
with open(r'db.txt', 'rb') as f,
open(r'db.txt.swap', 'wb') as wf:
while True:
cash_msg = f.readline().decode('utf-8')
if login_name in cash_msg:
*_ , z = cash_msg.strip().split(':')
if int(z) > int(money_cash):
money1 = int(z) - int(money_cash)
data = cash_msg.replace('{}'.format(z), '{}'.format(money1))
wf.write(data.encode('utf-8'))
else:
print('余额不足,提现失败')
return '余额不足,提现失败'
else:
wf.write('{}'.format(cash_msg).encode('utf-8'))
if len(cash_msg) == 0:
break
move_fuc()
print('提现成功')
query_fuc()
return '提现成功'
else:
print('还没有登录,请登录后再操作,输入P登录或者输入任意键退出')
inp_N = input('请输入符号:>>')
if inp_N == 'P':
login_func()
else:
print('退出')
break
# 主函数体
def main_func():
contests_atm = {
'0':register_func,
'1':login_fuc,
'2':recharge_fuc,
'3':take_fuc,
'4':cash_func,
'5':query_fuc,
'6':'6'
}
while True:
print("""
==============ATM====================
'0':注册
'1':登录
'2':充值
'3':转账
'4':提现
'5':余额
'6':退出
==============END====================
""")
cmd = input('请输入命令编号>>: ').strip()
if not cmd.isdigit():
print('必须输入命令编号的数字,傻叉')
continue
if cmd not in contests_atm:
print('输入的命令不存在')
continue
if cmd == '6':
print('已成功退出!')
break
contests_atm[cmd]()
main_func()
内心小总结:
不管做什么都需要心平气和,不能着急,越着急越想不到,二十分钟做不出来的登录锁定,放松下来一次搞定,心态决定一切!!