美文网首页
8.5传递任意数量的实参

8.5传递任意数量的实参

作者: user_Js | 来源:发表于2020-04-18 12:24 被阅读0次

    传递任意数量的实参

    def make_pizza(*toppings):
    """打印顾客点的所有配料"""
    print(toppings)

    make_pizza('huotuichang')
    make_pizza('mushrooms','green peppers','extra cheese')


    image.png

    def make_pizza(*toppings):
    """概述要制作的比萨"""
    print("Making a pizza with the following toppings:")
    for topping in toppings:
    print("- " + topping)

    make_pizza('huotuichang')
    make_pizza('mushrooms','green peppers','extra cheese')


    image.png

    结合使用位置实参和任意数量实参

    def make_pizza(size,*toppings):
    """概述要制作的比萨"""
    print("Making a " + str(size) +
    "-inch pizza with the following toppings:")
    for topping in toppings:
    print("- " + topping)

    make_pizza(16,'huotuichang')
    make_pizza(12,'mushrooms','green peppers','extra cheese')


    image.png

    使用任意数量的关键字实参

    def build_profile(first,last,**user_info):
    """创建一个字典,其中包含我们知道的用户的一切"""
    profile = {}
    profile['first_name'] = first
    profile['last_name'] = last
    for key, value in user_info.items():
    profile[key] = value
    return profile

    user_profile = build_profile('jiao','shuai',
    location= 'princetion',
    field= 'physics')
    print(user_profile)


    image.png

    三明治

    def make_sandwich(*items):
    """概述要加的东西"""
    print("I'll make you a great sandwich:")
    for item in items:
    print("...adding " + item + "in you sandwich:")
    print("Your sandwich is ready",'\n')

    make_sandwich('roast beef', 'cheddar cheese', 'lettuce', 'honey dijon')
    make_sandwich('turkey', 'apple slices', 'honey mustard')
    make_sandwich('peanut butter', 'strawberry jam')


    image.png

    用户简介

    def build_profile(first,last,**user_info):
    """创建一个字典,其中包含我们知道的用户的一切"""
    profile = {}
    profile['first_name'] = first
    profile['last_name'] = last
    for key, value in user_info.items():
    profile[key] = value
    return profile

    user_profile = build_profile('jiao','shuai',
    location= 'princetion',
    field= 'physics',
    language= 'python')
    print(user_profile)


    image.png

    汽车

    def make_car(manufacturer, model, **options):
    """创建一个字典,包含我们知道的车的一切"""
    car = {'manufacturer': manufacturer.title(),
    'model': model.title()}
    for option, value in options.items():
    car[option] = value

    return car
    

    my_outback = make_car('subaru', 'outback', color='blue', tow_package=True)
    print(my_outback)
    my_accord = make_car('honda', 'accord', year=1991, color='white',
    headlights='popup')
    print(my_accord)


    image.png

    相关文章

      网友评论

          本文标题:8.5传递任意数量的实参

          本文链接:https://www.haomeiwen.com/subject/kzbyuhtx.html