3.19作业
作业
作业要求:下述所有代码画图以及分析代码执行流程
1、以定义阶段为准,先画出名称空间的嵌套关系图
2、然后找到调用函数的位置,写出函数调用时代码的执行过程,涉及到名字的查找时,参照1中画好
的嵌套图,标明查找顺序,一层一层直到找到位置
题一到题七图片博客:
https://www.cnblogs.com/zhangtieshan/p/12528800.html
题目一:
# ===================题目一===================
input=333
def func():
input=444
func()
print(input)
题目一:
# ===================题目二===================
def func():
print(x)
x=111
func()
题目三:
# ===================题目三===================
x=1
def func():
print(x)
def foo():
x=222
func()
foo()
题目四:
# ===================题目四===================
input=111
def f1():
def f2():
# input=333
print(input)
input=222
f2()
f1()
题目五:
# ===================题目五===================
x=111
def func():
print(x) #
x=222
func()
题目六:
# ===================题目六===================
x=111
def foo():
print(x,)
def bar():
print(x)
foo()
bar()
题目七:
# ===================题目七===================
x=1
def func2():
func1()
x=2
def func1():
print(x)
x=3
func2()
题目八:
1.
如下全局变量记录了当前登录用户,编写登录功能,一旦用户登录成功,则将全局变量赋值为当前登录的用户名
login_user = None
def check_login():
'''
:param new_user: 当前登录用户的用户名
:param pwd: 当前登录用户的密码
:return:
'''
new_user = input('输入用户名: ').strip()
pwd = input('输入密码: ').strip()
if new_user == 'tom' and pwd == '123':
global login_user
login_user = new_user
check_login()
print(f'当前登录用户:{login_user}')
2.
针对之前编写的查询余额的功能,添加额外的逻辑:如果用户没有登录,则先执行登录功能
user_info = {'user': None}
def check_money(username):
dict1 = {}
with open('db3.txt', 'rt', encoding='utf-8') as f1:
for line in f1:
user, money = line.strip().split(':')
dict1[user] = int(money)
if not user_info.get('user') != user_name:
login()
if username not in dict1:
return
if not user_info.get('user') == user_name:
print(dict1.get(user))
tage = False
def login():
username = input('请输入用户名: ').strip()
password = input('请输入密码: ').strip()
with open('u.txt', 'r', encoding='utf-8') as f2:
for line in f2:
user, pwd = line.strip().split(':')
if username == user and password == pwd:
user_info['user'] = username
print('登录成功!')
else:
print('登录失败!')
break
tage = True
while tage:
user_name = input('输入需要查询的用户: ').strip()
check_money(user_name)
文件db3.txt:
tank:1999
egon:1000
文件u.txt:
tank:111
egon:222