《Python编程:从入门到实践》第八章8.4节和8.5课后作业

本文代码是在jupyter中实现的,仅为了自我督促学习python之用。
8-9 魔术师:创建一个包含魔术师名字的列表,并将其传递给一个名为show_magicians() 的函数,这个函数打印列表中每个魔术师的名字。

代码:

def show_magicians(names):
    for name in names:
        print(name)
        
magicians = ['Harry Houdini', 'David Copperfield', 'Criss Angel']
show_magicians(magicians)

运行结果:

Harry Houdini
David Copperfield
Criss Angel

8-10 了不起的魔术师:在你为完成练习 8-9 而编写的程序中,编写一个名为make_great() 的函数,对魔术师列表进行修改,在每个魔术师的名字中都加入字样“the Great”。调用函数 show_magicians() ,确认魔术师列表确实变了。

代码:

def make_great(magicians_name, magicians_new):
    """对魔术师列表magicians_name中的每个魔术师前面添加'The Great',得到新的魔术师列表——magicians_new"""
    while magicians_name:
        current_magician = "The Great " + magicians_name.pop()
        # 逐个删除magicians_name列表中的元素,并增加前缀“The Great”,然后添加进magicians_new列表中
        magicians_new.append(current_magician)

def show_magicians(magicians_new):
    """打印魔术师列表中的所有元素"""
    print("\nThe top three magicians in the world are: ")
    for magician_new_name in magicians_new:
        print(magician_new_name)

magicians_name = ['Criss Angel', 'David Copperfield', 'Harry Houdini']
magicians_new = []

make_great(magicians_name, magicians_new)
show_magicians(magicians_new)

运行结果:

The top three magicians in the world are: 
The Great Harry Houdini
The Great David Copperfield
The Great Criss Angel

8-11 不变的魔术师:修改你为完成练习 8-10 而编写的程序,在调用函数make_great() 时,向它传递魔术师列表的副本。由于不想修改原始列表,请返回修改后的列表,并将其存储到另一个列表中。分别使用这两个列表来调用 show_magicians() ,确认一个列表包含的是原来的魔术师名字,而另一个列表包含的是添加了字样“the Great”的魔术师名字。

代码:

def make_great(magicians_name, magicians_new):
    """对魔术师列表magicians_name中的每个魔术师前面添加'The Great',得到新的魔术师列表——magicians_new"""
    while magicians_name:
        current_magician = "The Great " + magicians_name.pop()
        # 逐个删除magicians_name列表中的元素,并增加前缀“The Great”,然后添加进magicians_new列表中
        magicians_new.append(current_magician)

def show_magicians(magicians_new):
    """打印魔术师列表中的所有元素"""
    print("\nThe top three magicians in the world are: ")
    for magician_new_name in magicians_new:
        print(magician_new_name)

magicians_name = ['Criss Angel', 'David Copperfield', 'Harry Houdini']
magicians_new = []

make_great(magicians_name[:], magicians_new) # magicians_name[:]表示magicians_name的副本
show_magicians(magicians_name)
show_magicians(magicians_new)

运行结果:


The top three magicians in the world are: 
Criss Angel
David Copperfield
Harry Houdini

The top three magicians in the world are: 
The Great Harry Houdini
The Great David Copperfield
The Great Criss Angel

8-12 三明治:编写一个函数,它接受顾客要在三明治中添加的一系列食材。这个函数只有一个形参(它收集函数调用中提供的所有食材),并打印一条消息,对顾客点的三明治进行概述。调用这个函数三次,每次都提供不同数量的实参。

代码:

def pizza(*toppings): # *toppings表示创建了一个名为toppings的空元组,用来打包储存输入的元素
    """打印顾客需要在披萨中添加的配菜"""
    print("\nYou ordered a pizza including the following: ")
    for topping in toppings:
        print("- " + topping)
        
pizza('pepperoni')
pizza('bacon', 'cheese')
pizza('mushrooms', 'green peppers', 'extra cheese')

运行结果:


You ordered a pizza including the following: 
- pepperoni

You ordered a pizza including the following: 
- bacon
- cheese

You ordered a pizza including the following: 
- mushrooms
- green peppers
- extra cheese

8-13 用户简介:复制前面的程序 user_profile.py ,在其中调用 build_profile() 来创建有关你的简介;调用这个函数时,指定你的名和姓,以及三个描述你的键—值对。

代码:

def build_profile(first, last, **user_info): # **user_info表示创建一个名为profile的空字典,用来储存输入的键-值对
    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('Li', 'Hua', location='China', nationality='Han', specialty='run')
print(user_profile)

运行结果:

{'first_name': 'Li', 'last_name': 'Hua', 'location': 'China', 'nationality': 'Han', 'specialty': 'run'}

8-14 汽车:编写一个函数,将一辆汽车的信息存储在一个字典中。这个函数总是接受制造商和型号,还接受任意数量的关键字实参。这样调用这个函数:提供必不可少的信息,以及两个名称 — 值对,如颜色和选装配件。这个函数必须能够像下面这样进行调用:
car = make_car(‘subaru’, ‘outback’,color=‘blue’, tow_package=True)
打印返回的字典,确认正确地处理了所有的信息。

代码:

def make_car(first, last, **car_info):
    """将一辆汽车的信息储存在一个字典里,并打印该字典"""
    message = {}
    message['manufacturer'] = first
    message['model'] = last
    for key, value in car_info.items():
        message[key] = value
    return message

car_message = make_car('subaru', 'outback', color='blue', accessories='driving recorder',tow_package=True)
print(car_message)

运行结果:

{'manufacturer': 'subaru', 'model': 'outback', 'color': 'blue', 'accessories': 'driving recorder', 'tow_package': True}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值