#global关键字,可以在局部名称空间修改全局不可变类型
x = 1deffunc():
global x
x = 2
func()
print(x) # 2# 在局部名称空间修改可变类型不需要加关键字
l = [1,2]
deffunc():
l.append(3)
func()
print(l) # [1, 2, 3]# 局部名称空间嵌套的情况下,可以使用nonlocal关键字,在内内层修改外层空间不可变类型,可变类型可以直接修改deffunc():
x = 1
l = [1]
deffunc2()
nonlocal x
x = 2
l.append(2)
func2()
print(x)
print(l)
func()
>>>2
>>>[1, 2]
函数名的多种用法
deffunc():
print('func')
# 用法1:函数名可以当变量名赋值print(func)
>>> <function func at 0x0000028AF4D84B70>
# 可以让func中的地址在绑定一个新的变量名,并且新的变量名也可以调用该函数
res = func
res() 等价于 func()
# 用法2:函数名可以当成函数的实参deffunc2(a):
print(a)
func2(func)
>>> <function func at 0x0000016CEA2F4B70>
# 用法3:函数名可以当函数的返回值deffunc3(a):
print('func3')
return func
print(func3)
>>><function func3 at 0x000002D2422CCC80>
# 用法4:函数名可以作为容器的元素deffunc3(a):
print('func3')
return func
l = [1,2,func3]
print(l)
>>>[1, 2, <function func3 at 0x000002B73A7CCC80>]
函数的嵌套
# 嵌套调用
获取两个数中的较大数
a = 1
b = 2defindex(a,b):
if a>b:
return a
return b
print(index(a,b)) # 2# 返回四个值中的较大值defmany_max(a,b,c,d)
res = index(a,b)
res2 = index(c,d)
res3 = index(res,res2)
作业
点击展开
import os
defadd_employee():
id_input = input('请输入新增员工id:').strip()
withopen(r'a.txt', 'r', encoding='utf8') as f1:
for info in f1:
if info.split('|')[0] == id_input:
print('员工已存在')
else:
name_input = input('请输入员工姓名:').strip()
age_input = input('请输入员工年龄:').strip()
salary_input = input('请输入员工薪资:').strip()
withopen(r'a.txt', 'a', encoding='utf8') as f2:
f2.write(f'{id_input}|{name_input}|{age_input}|{salary_input}\n')
print('员工添加成功')
breakdefselect_employee():
id_input = input('请输入员工编号:').strip()
withopen(r'a.txt') as f1:
for info in f1:
if id_input in info:
res = info.strip('\n')
print(f"编号:{res.split('|')[0]}")
print(f"姓名:{res.split('|')[1]}")
print(f"年龄:{res.split('|')[2]}")
print(f"薪资:{res.split('|')[3]}")
else:
print('未找到该员工')
defrevise_salary():
id_input = input('请输入员工编号:')
withopen(r'a.txt', 'r', encoding='utf8') as f1:
for info in f1:
salary = info.strip('\n')
if id_input in salary:
salary_now = input('请输入当前薪资')
salary_input = input('请输入修改后的薪资:')
withopen('a.txt') as f2:
data = f2.read()
withopen('a.txt', 'w', encoding='utf8') as f3:
f3.write(data.replace(salary_now, salary_input))
defselect_all():
withopen(r'a.txt') as f:
for info in f:
res = info.strip('\n')
print(f"编号:{res.split('|')[0]}")
print(f"姓名:{res.split('|')[1]}")
print(f"年龄:{res.split('|')[2]}")
print(f"薪资:{res.split('|')[3]}")
print('**************')
defdelete_employee():
id_input = input('请输入删除的员工编号:')
withopen(r'a.txt') as f1:
for info in f1:
res = info.split('|')
if id_input notin res:
withopen(r'b.txt', 'a', encoding='utf8') as f2:
f2.write(f'{res[0]}|{res[1]}|{res[2]}|{res[3]}')
os.remove('a.txt')
os.rename('b.txt.swap', 'a.txt')
dict_info = {'1': add_employee, '2': select_employee, '3': revise_salary, '4': select_all,
'5': delete_employee}
whileTrue:
print('1.添加员工信息\n2.查询特定员工\n3.修改员工薪资\n4.查询所有员工\n5.删除特定员工')
choice = input('请输入您的选择:')
if choice in dict_info:
res = dict_info[choice]
res()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)