Python Day15 作业

 

 

 

 

题目八:


import os
def login():
print('开始登录')
login_t = False
tag = False
count = 0
while count < 3:
in_name = input('输入名字:')
in_password = input('请输入密码:')
with open('db.txt', encoding='utf-8')as f, open('.db.txt.swap', mode='w', )as f1:
for i in f:
name, password, money, lock = i.strip('\n').split(':')
if in_name == name:
tag = True
print('用户存在,', end='')
if password == in_password and lock == 'lock_on':
print('账号被锁定,无法登陆')
count = 3
elif password == in_password and lock != 'lock_on':
print('登录成功')
count = 3
login_t = True
atm_name = in_name
else:
print('密码错误,三次后将被锁定')
count += 1
if count == 3:
print('三次错误被锁定')
lock = 'lock_on'
i = f'{name}:{password}:{money}:{lock}\n'
f1.write(i)
else:
if not tag:
print('用户不存在')
os.remove('db.txt')
os.rename('.db.txt.swap', 'db.txt')
return login_t, atm_name


# 4、编写注册功能
def register():
print('注册')
in_name = input('输入名字:')
in_password = input('请输入密码:')
with open('db.txt', mode='a', encoding='utf-8') as f1, open(r'db.txt', encoding='utf-8')as f2:
for i in f2:
a = i.strip('\n').split(':')
if in_name == a[0]:
print('已注册')
break
else:
f1.write(f'{in_name}:{in_password}:0:lock_off\n')
print('注册成功')


def out():
global tag
tag = False


def atm(user):
atm_f = {
1: take_money,
2: invest_money,
3: remove_money,
4: show_money
}
info = """
1 取钱
2 充钱
3 转钱
4 查询余额
"""
print(info)
chose = input('输入选择:')
if int(chose) in atm_f:
atm_f.get(int(chose))(user)
else:
print('输入不正确的选择')


def change_dbm(user, c_money):
with open('db.txt', mode='r', encoding='utf-8')as f, \
open('.db.txt.swap', mode='w')as f2:
for i in f:
a = i.strip('\n').split(':')
name, password, money, lock = a
if user == name:
money = int(money)
money = money + int(c_money)
i = f'{name}:{password}:{money}:{lock}\n'
f2.write(i)
os.remove('db.txt')
os.rename('.db.txt.swap', 'db.txt')
return money


def take_money(user):
c_money = input('输入要取的钱')
money = -int(c_money)
change_dbm(user, money)


def invest_money(user,*args):
if not args[1]:
c_money=args[0]
else:
c_money = input('输入要存入的钱')
change_dbm(user, c_money)


def remove_money(user):
c_money = input('输入要转账的钱:')
t_user=input('转账对象:')
invest_money(t_user,c_money,False)
money = -int(c_money)
change_dbm(user, money)


def show_money(user):
print(change_dbm(user,c_money='0'))


res = {
out: 0,
login: 1,
register: 2
}
tag = True
while tag:
msg = """
0 退出
1 登录
2 注册
"""
print(msg)
cmd = input('请输入命令编号>>: ').strip()
for i in res:
if str(res.get(i)) == cmd:
info = i()
if info:
if_login, user = info
if if_login:
atm(user)

猜你喜欢

转载自www.cnblogs.com/AaronY/p/12527662.html