周末作业
编写ATM程序实现下述功能,数据来源于文件db.txt
# 0、注册功能:用户输入账号名、密码、金额,按照固定的格式存入文件db.txt
user_info = {'user': None}
def register():
name=input('your name:').strip()
pwd=input('you password:').strip()
pwd1=input('once again:').strip()
if pwd1 == pwd:
print(f'{name}注册成功')
else:
print('注册失败')
# register()
# 1、登录功能:用户名不存在,要求必须先注册,用户名存在&输错三次锁定,登录成功后记录下登录状态(提示:可以使用全局变量来记录)
def login():
name=input('your name:').strip()
pwd=input('your password:').strip()
with open('users.txt','r',encoding='utf-8') as f:
for line in f:
user,password=line.strip().split(':')
if name == user and pwd == password:
user_info['user']=name
print('登录成功')
break
else:
print('登录失败')
# login()
# 下述操作,要求登录后才能操作
# 1、充值功能:用户输入充值钱数,db.txt中该账号钱数完成修改
def pay_money():
dic={}
username=input('请输入充值用户:').strip()
with open('db.txt','r',encoding='utf-8') as f:
for line in f:
user,money=line.strip().split(':')
dic[user]=int(money)
if not user_info.get('user'):
login()
if username not in dic:
print('用户不存在,结束程序')
return
while True:
money=input('充值多少:').strip()
if not money.isdigit():
print('请输入纯数字')
continue
money=int(money)
dic[username]+= money
with open('db.txt','w',encoding='utf-8') as f:
for user,money in dic.items():
f.write(f'{user}:{money}\n')
print('充值成功')
break
# pay_money()
# 2、转账功能:用户A向用户B转账1000元,db.txt中完成用户A账号减钱,用户B账号加钱
def transfer():
a_user=input('请输入转账用户:').strip()
b_user=input('请输入收款用户:').strip()
transfer_moner=int(input('转多少:').strip())
dic={}
with open('db2.txt','r',encoding='utf-8') as f:
for line in f:
user,money=line.strip().split(':')
dic[user]=int(money)
if not user_info.get('user'):
login()
if a_user not in dic:
print('转账用户不存在')
return
if b_user not in dic:
print('收款用户不存在')
return
print('转账前:',dic)
if dic.get(a_user) >= transfer_moner:
dic[a_user] -= transfer_moner
dic[b_user] += transfer_moner
print('转账后:',dic)
with open('db2.txt','w',encoding='utf-8') as f:
for user,money in dic.items():
f.write(f'{user}:{money}\n')
# 3、提现功能:用户输入提现金额,db.txt中该账号钱数减少
def withdraw():
username=input('提现用户:').strip()
get_money=int(input('提现多少:').strip())
dic={}
with open('db3.txt','r',encoding='utf-8') as f:
for line in f:
user,money=line.strip().split(':')
dic[user]=int(money)
if not user_info.get('user'):
login()
if username not in dic:
print('用户不存在')
return
if dic.get(username) >= get_money:
dic[username] -= get_money
print(dic)
with open('db3.txt','w',encoding='utf-8') as f:
for user,money in dic.items():
f.write(f'{user}:{money}\n')
# 4、查询余额功能:输入账号查询余额
def check_money():
dic={}
username = input('查询账户:').strip()
with open('db.txt','r',encoding='utf-8') as f:
for line in f:
user,money=line.strip().split(':')
dic[user]=int(money)
if not user_info.get('user'):
login()
if username not in dic:
print('用户不存在')
return
print(dic.get(username))
dic={
'0':('退出',None),
'1':('登录',login),
'2':('注册',register),
'3':('充值',pay_money),
'4':('转账',transfer),
'5':('提现',withdraw),
'6':('查余额',check_money),
}
while True:
for line in dic:
print(line,dic[line][0])
cmd=input('请输入指令:').strip()
if not cmd.isdigit():
print('请输入纯数字指令,傻子')
continue
if cmd == '0':
break
if cmd in dic:
dic[cmd][1]()
else:
print('傻子,没有这个指令')