python第七次练习

例题8-13 用户简介

  描述:任意数量的关键字实参创建用户简介,名和姓加描述键值对。
  代码:

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('albert', 'einstein', 
                             location='princeton',
                             field='physics')
print(user_profile)
my_profile = build_profile('ponyo', 'hu',
                           location='Guangzhou',
                           age='20',
                           gender='female')
print(my_profile)

  结果:
这里写图片描述

例题8-12 三明治

  描述:任意数量的实参,只有一个形参(收集所有食材)。
  代码:

def make_sandwich(*toppings):
    print("\nMaking a sandwich with the following toppings:")
    for topping in toppings:
        print("-" + topping)
make_sandwich("beef", "mushroom" ,"onions")
make_sandwich("beef")
make_sandwich("beef", "onions")

  结果:
这里写图片描述

例题8-8 用户的专辑

  描述:使用while输入歌手和专辑名称,返回字典。
  代码:

def make_album(name, album_name, amount = ''):
    album = {'name': name, 'album_name': album_name}
    if amount:
        album['amount'] = amount
    return album
print(make_album('Jay Chou', 'Jay'))
print(make_album('coldplay', 'Parachutes'))
print(make_album('The Chainsmokers', 'Bouquet', 5))

  结果:
这里写图片描述

发现的问题:

  1. 记得传递参数
  2. 8-13
  3. 让参数变作为可选的,设置默认值为‘’空字符串
    if amount:
    album[‘amount’] = amount
    然后输入什么值都行???
  4. 练习8.8
  5. 注意在什么时候用“”,什么时候用‘’
  6. 使用任意数量的关键字实参,可看做字典传进去,不要忘记.items()
    调用时,注意‘关键字’不需要加引号。
  7. 8-14
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值