![](https://img2020.cnblogs.com/blog/2053562/202007/2053562-20200720211137241-1613490379.png)
![](https://img2020.cnblogs.com/blog/2053562/202007/2053562-20200720211144647-762532859.png)
![](https://img2020.cnblogs.com/blog/2053562/202007/2053562-20200720211150930-1126570499.png)
![](https://img2020.cnblogs.com/blog/2053562/202007/2053562-20200720211157987-104453974.png)
![](https://img2020.cnblogs.com/blog/2053562/202007/2053562-20200720211204787-151619899.png)
![](https://img2020.cnblogs.com/blog/2053562/202007/2053562-20200720211214125-607073749.png)
# 1、如下全局变量记录了当前登录用户,编写登录功能,一旦用户登录成功,则将全局变量赋值为当前登录的用户名
log_info = None
def login():
user_dic = {}
global log_info
with open('a.txt', mode='rt', encoding='utf-8')as f:
for line in f:
user, pwd, money = line.strip().split(":")
user_dic[user] = [pwd, money]
inp_name = input("your name:").strip()
inp_pwd = input("your password:").strip()
if inp_name == user and inp_pwd == pwd:
print('登录成功')
log_info = user
break
else:
print("账号密码错误")
login()
# 2、针对之前编写的查询余额的功能,添加额外的逻辑:如果用户没有登录,则先执行登录功能
def check_money(user):
if log_info:
with open('a.txt', 'r', encoding='utf-8')as f:
for line in f:
if user in line:
user, pwd, money = line.strip().split(":")
print(money)
else:
print("用户未登录,请先登录")
login()
check_money("liu")