💖Puthon---函数💖

47 阅读1分钟
#===================学生管理系统=======================================
#1.显示学生管理系统
def show_menu():
    """显示学生管理系统"""
    print("----------------------")
    print("|1.添加学生信息")
    
    print("|2.查询学生信息")
    print("|3.退出系统")
    print("-----------------------")


show_menu()
choice = input("请输入您的选择:")
show_menu()

#------------------------参数传递------------------------------------
#  形式参数(形参):定义函数设置的参数
#  实际参数(实参):调用函数传入的参数
#-------------------1、 位置参数的传递-----------------------------------
print("------------位置参数的传递---------------")
def get_max(a,b):
    """比较a和b的大小"""
    if a>b:
        print(a,"比较大")
    else:
        print(b,"比较大")
get_max(5,20)

# ------------------2、关键字参数的传递-----------------------------------------
# "形参=实参”,将 实参 按照对应的 “关键字” 传递给 形参
print("------------关键字参数的传递----------------")
def connect(ip,port):
    """连接设备"""
    print(f"设备{ip}:{port}连接")
connect(ip="127.0.01",port=8080)
# ------------------3、默认参数的传递-------------------------
# 函数定义时可以指定形参的默认值,调用可以直接用默认值,也可以修改
# 注意:默认形参后面不能有非默认值形参!!!!
print("------------默认参数的传递----------------")
def connect(ip,port=8080):
    """连接设备"""
    print(f"设备{ip}:{port}连接")
connect(ip="127.0.02")
connect(ip="127.0.03",port=3386)
# ------------------4、参数的打包与解包------------------------------
print("-------------参数的打包与解包----------------")
#-----------打包-------------
print("----------------------打包-----------------")
#函数定义时无法确定接受多少个数据
# “*args"或”**kwargs"
def test(*args):
    print(args)
test("Python","a",23,56)
# -------------------解包---------------------------------
# 函数调用时接收实参是元祖,使用“*”
# 函数调用时接收实参是字典,使用“**”,接收值
print("--------------------解包-----------------")
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,"c":33,"d":44,"e":55}
test(**nums)