3.22周末作业

# 编写ATM程序实现下述功能,数据来源于文件db.txt
# 0、注册功能:用户输入账号名、密码、金额,按照固定的格式存入文件db.txt
# def regeister():
# dic={}
# with open('db.txt','r',encoding='utf-8')as f:
# for line in f :
# name,pwd,money=line.strip().split(':')
# dic[name]=[pwd,money]
# while 1:
# username = input('请输入用户名: ').strip()
# if not username in dic:
# password = input('请输入密码: ').strip()
# re_password = input('请输入密码: ').strip()
# count=input('请输入金钱:').strip()
# if password==re_password:
# print('注册成功')
# with open("db.txt","a",encoding='utf-8') as w:
# w.write(f'{username}:{password}:{count}\n')
# break
# else:
# print('密码不一致')
# else:
# print('账户已存在')


# regeister()


# 1、登录功能:用户名不存在,要求必须先注册,用户名存在&输错三次锁定,登录成功后记录下登录状态(提示:可以使用全局变量来记录)
user_info = {'user': None}
def login():
dic = {}
with open('db.txt', 'r', encoding='utf-8')as f:
for line in f:
name, pwd, money = line.strip().split(':')
dic[name] = [pwd, money]
number=0
while number<3:
username = input('请输入用户名: ').strip()
if username in dic:
password = input('请输入密码: ').strip()
if password==dic[username][0]:
print('登录成功')
user_info['user']=username
break
else:
print('登录失败')
number+=1
if number==3:
print(f'账户{username}被锁定')
break
else:
print('===开始注册====')
regeister()
return login()



# login()





# 下述操作,要求登录后才能操作
# 1、充值功能:用户输入充值钱数,db.txt中该账号钱数完成修改
# def pay_money(x):
# dic = {}
# with open('db.txt', 'r', encoding='utf-8')as f:
# for line in f:
# name, pwd, money = line.strip().split(':')
# dic[name] = [pwd, int(money)]
# while 1:
# username = input('请输入用户名: ').strip()
# if not user_info.get('user'):
# login()
# else:
# dic[username][1]+=x
# with open('db.txt', 'r+', encoding='utf-8')as f2:
# for i in dic:
# f2.write(f'{i}:{dic[i][0]}:{dic[i][1]}\n')
# break
# pay_money(10)


# 2、转账功能:用户A向用户B转账1000元,db.txt中完成用户A账号减钱,用户B账号加钱
def transfer(A,B,x):
dic = {}
with open('db.txt', 'r', encoding='utf-8')as f:
for line in f:
name, pwd, money = line.strip().split(':')
dic[name] = [pwd, int(money)]
while 1:
if not user_info.get('user'):
login()
if A not in dic:
print('转账用户不存在')
return

if B not in dic:
print('收款用户不存在')
return

if dic[A][1]>= x:

dic[A][1] -= x
dic[B][1] +=x
with open('db.txt', 'r+', encoding='utf-8')as f2:
for i in dic:
f2.write(f'{i}:{dic[i][0]}:{dic[i][1]}\n')
break
# transfer('egon','tank',20)



# 3、提现功能:用户输入提现金额,db.txt中该账号钱数减少
def withdraw(x):
dic = {}
with open('db.txt', 'r', encoding='utf-8')as f:
for line in f:
name, pwd, money = line.strip().split(':')
dic[name] = [pwd, int(money)]
while 1:
username = input('请输入用户名: ').strip()
if not user_info.get('user'):
login()
else:
dic[username][1]-=x
with open('db.txt', 'r+', encoding='utf-8')as f2:
for i in dic:
f2.write(f'{i}:{dic[i][0]}:{dic[i][1]}\n')
break










# 4、查询余额功能:输入账号查询余额
def check_money(F):
dic = {}
with open('db.txt', 'r', encoding='utf-8')as f:
for line in f:
name, pwd, money = line.strip().split(':')
dic[name] = [pwd, int(money)]
while 1:
if not user_info.get('user'):
login()
else:
print(dic[F][1])
break



check_money('tank')

猜你喜欢

转载自www.cnblogs.com/chenyoupan/p/12548397.html
今日推荐