# 作业要求:下述所有代码画图以及分析代码执行流程
# 1、以定义阶段为准,先画出名称空间的嵌套关系图
# 2、然后找到调用函数的位置,写出函数调用时代码的执行过程,涉及到名字的查找时,参照1中画好
# 的嵌套图,标明查找顺序,一层一层直到找到位置
# ===================题目一===================
# input=333
# def func():
# input=444
# func()
# print(input)
# 内置input=内存地址,然后全局input=333,func()函数调用在func局部产生input=444,函数调用完成后销毁,然后执行print(input),
# 在全局找到input = 333,返回结果
# ===================题目二===================
# def func():
# print(x)
# x=111
#
# func()
# 在函数调用前,全局有x=111,然后函数调用print(x),func局部没找到,去全局找到x=111,返回结果
# ===================题目三===================
# x=1
# def func():
# print(x)
#
#
# def foo():
# x=222
# func()
# foo()
# 全局x=1,调用函数foo,foo局部x=222,调用函数func,func局部print(x),没找到x,去全局找到x=1,返回结果
# ===================题目四===================
# input=111
# def f1():
# def f2():
# print(input)
# input=222
# f2()
#
# f1()
#内置input = 内存地址,全局input = 111,f1函数调用,定义函数f2,input=222,调用函数f2,f2print(input),没找到去f1
# 局部找到input=222,返回结果
# ===================题目五===================
# x=111
# def func():
# print(x) #
# x=222
#
# func()
# 全局x=111,func调用,func局部先打印x,再去给x绑定222,报错,赋值前引用的局部变量“x”
# ===================题目六===================
# x=111
#
# def foo():
# print(x,)
#
# def bar():
# print(x)
#
# foo()
# bar()
# foo与bar函数调用都是在各自的局部没找到去全局找到x=111,返回结果
# ===================题目七===================
# x=1
# def func2():
# func1()
#
# x=2
# def func1():
# print(x)
#
# x=3
#
# func2()
# 在调用func2之前全局x=3, func2函数调用后,函数体代码调用函数func1,func1函数调用后print(x)局部没找到去全局找到x=3,返回结果
# ===================题目八===================
# 1、如下全局变量记录了当前登录用户,编写登录功能,一旦用户登录成功,则将全局变量赋值为当前登录的用户名
# login_user=None
# login_user=None
#
# def login():
# print('欢迎来到登录界面'.center(30, '*'))
# user = input('请输入用户名:')
# with open(r'user.txt','r',encoding='utf-8') as f:
# for line in f:
# username,password,money = line.strip().split(':')
# if user in username:
# pwd = input('请输入密码:')
# if pwd == password:
# print('登录成功')
# global login_user
# login_user = user
# break
# else:
# print('密码错误')
# else:
# print('该用户不存在')
#
# # login()
# print(f'当前登录用户为{login_user}')
# 2、针对之前编写的查询余额的功能,添加额外的逻辑:如果用户没有登录,则先执行登录功能
login_user=None
def login():
print('欢迎来到登录界面'.center(30, '*'))
user = input('请输入用户名:')
with open(r'user.txt','r',encoding='utf-8') as f:
for line in f:
username,password,money = line.strip().split(':')
if user in username:
pwd = input('请输入密码:')
if pwd == password:
print('登录成功')
global login_user
login_user = user
break
else:
print('密码错误')
else:
print('该用户不存在')
def check_the_balance(login_user):
'''查询余额'''
if login_user == None:
print('请先登录')
return
else:
print('查询余额功能'.center(30, '*'))
with open(r'user.txt', 'r', encoding='utf-8') as f:
for line in f:
if line.startswith(login_user):
user, pwd, money = line.strip().split(':')
print('用户剩余余额{}元'.format(money))
def sign_out():
'''退出程序'''
exit()
dic = {
'0': sign_out,
'1':login,
'2':check_the_balance
}
while True:
print('''
0---退出
1---登录
2---查询余额
''')
cmd = input('请输入命令:').strip()
if not cmd.isdigit():
print('必须输入数字指令,sb')
continue
if cmd not in dic:
print('指令有误,请重新输入')
continue
if cmd == '2':
dic.get(cmd)(login_user)
else:
dic.get(cmd)()