def show_menu():
print("_________________________")
print("|1.添加学生信息")
print("|2.查询学生信息")
print("|3.退出系统")
print("_________________________")
def add_student(name,age,grade,student_id =None):
if student_id is None:
student_id = len(students)+1001
student = {
"name":name,
"age":age,
"grade":grade,
"id":student_id
}
students.append(student)
print(f"成功添加学生:{name}(id:{student_id})")
show_menu()
choice = input("请输入您的选择:")
show_menu()
参数传递
1.形式参数:定义函数设置的参数
2.实际参数:调用函数传入的参数
(1)位置参数的传递
"""函数调用时 实参 按照 位置依次传递给 形参"""
def get_max(a,b):
"""比较a和b大小"""
if a>b:
print(a,"比较大")
else:
print(b,"比较大")
get_max(5,20)
(2)关键字参数的传递
"""形参 = 实参,将 实参 按照对应的关键字传递给 形参 """
def connect(ip,port):
"""连接设备"""
print(f"设备{ip}:{port}连接")
connect(ip="127.0.01",port=8080)
(3)默认参数的传递
"""函数定义时可以指定形参的默认值,调用可直接用默认值,也可以修改"""
"""注意:默认形参后面不能有非默认形参!!!"""
def connect(ip,port=8080):
print(f"设备{ip}:{port}连接")
connect(ip="127.0.02")
connect(ip="127.0.03",port=3306)
(4)参数的打包与解包
#打包
"""函数定义时无法确定接收多少个数据"""
#"*args"或"**kwargs"
def test(*args):
print(args)
test("Python","a",23,56)
#解包
#函数调用时接受实参是元组,使用“*”
#函数调用时接受实参是字典,使用“**”
def test(a,b,c,d,e):
print(a,b,c,d,e)
nums = (12,22,33,44,55)
test(*nums)
nums = {"a":12,"b":22,"d":44,"e":55}
test(**nums)
(5)混合传递
#位置参数,默认参数,*args.关键字——only参数,**kwargs
#仅关键字参数
#定义优先级
def func(a,b=2,*args,c,**kwargs):
print(a,b,args,c,kwargs)
#正确调用
func("A","B","Math","Science",c="C",city="Hubei")
#错误调用1:缺少必要的关键字参数
#func("A",b="B","Math","Science")
#错误调用2:重复传参
#func("A","B",e="Math",b=16)
#错误调用3:位置参数在关键字参数之后
#func(a="A",b="B","Math","Science"