高级编程技术_课后作业(八)

说明

下面为课本上第八章动手试一试中的部分习题

8-1

题目:
消息 :编写一个名为display_message() 的函数,它打印一个句子,指出你在本章学的是什么。调用这个函数,确认显示的消息正确无误。


code:
def display_message():
    print("I am learing how to define functions in Python")

display_message()

result:



8-2

题目:
喜欢的图书 :编写一个名为favorite_book() 的函数,其中包含一个名为title 的形参。这个函数打印一条消息,如One of my favorite books is Alice in Wonderland 。调用这个函数,并将一本图书的名称作为实参传递给它。

code:
def favorite_book(title):
    print("One of my favorite books is " + title)

favorite_book("Alice in Wonderland")

result:


8-3

题目:
T恤 :编写一个名为make_shirt() 的函数,它接受一个尺码以及要印到T恤上的字样。这个函数应打印一个句子,概要地说明T恤的尺码和字样。使用位置实参调用这个函数来制作一件T恤;再使用关键字实参来调用这个函数。

code:
def make_shirt(size, words):
    print("The T-shirt's size is " + size + ", and the words on it are " + words)

make_shirt("L", "hhh")
make_shirt(words = "hhh", size = "L")

result:


8-4

题目:
大号T恤 :修改函数make_shirt() ,使其在默认情况下制作一件印有字样“I love Python”的大号T恤。调用这个函数来制作如下T恤:一件印有默认字样的大号T恤、一件印有默认字样的中号T恤和一件印有其他字样的T恤(尺码无关紧要)。

code:
def make_shirt(size = "L", words = "I love Python"):
    print("The T-shirt's size is " + size + ", and the words on it are " + words)

make_shirt("L")
make_shirt("M")
make_shirt(words = "I love C++")

result:


8-7

题目:
专辑 :编写一个名为make_album() 的函数,它创建一个描述音乐专辑的字典。这个函数应接受歌手的名字和专辑名,并返回一个包含这两项信息的字典。使用这个函数创建三个表示不同专辑的字典,并打印每个返回的值,以核实字典正确地存储了专辑的信息。给函数make_album() 添加一个可选形参,以便能够存储专辑包含的歌曲数。如果调用这个函数时指定了歌曲数就将这个值添加到表示专辑的字典中。调用这个函数,并至少在一次调用中指定专辑包含的歌曲数。

code:
def make_album(name, album, number=''):
    music = {'singer':name, 'album':album}
    if number:
        music['number'] = number
    return music

print(make_album('Jay', 'Jay'))
print(make_album("IU", "chat_shire", '2'))
print(make_album('IU', 'Platte'))

result:

8-8

题目:
用户的专辑 :在为完成练习8-7编写的程序中,编写一个while 循环,让用户输入一个专辑的歌手和名称。获取这些信息后,使用它们来调用函数make_album() ,并将创建的字典打印出来。在这个while 循环中,务必要提供退出途径。

code:
def make_album(name, album, number=''):
    music = {'singer':name, 'album':album}
    if number:
        music['number'] = number
    return music

print("Enter q to quit anytime")
while True:
    singer = input("Enter the name of singer\n")
    if singer == 'q':
        break;
    album = input("Enter one of " + singer + "'s album\n")
    if singer == 'q':
        break;
    print(make_album(singer, album))

result:


8-10

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

code:
def make_great(magicians):
    for i in range(len(magicians)):
        magicians[i] = "the Great " + magicians[i]

def show_magicians(magicians):
    for magician in magicians:
        print(magician)

magicians = ['zero', 'Jay', 'hhh']
print("The magicians are:")
show_magicians(magicians)
make_great(magicians)
print("Now they are:")
show_magicians(magicians)

result:


8-11

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

code:

def make_great(magicians):
    for i in range(len(magicians)):
        magicians[i] = "the Great " + magicians[i]
    return magicians

def show_magicians(magicians):
    for magician in magicians:
        print(magician)

magicians = ['zero', 'Jay', 'hhh']
print("The magicians are:")
show_magicians(magicians)
great_magicians = make_great(magicians[:])
print("The magicians are:")
show_magicians(magicians)
print("The great magicians are:")
show_magicians(great_magicians)

result:


8-12

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

code:
def sandwitch(*ingredients):
    print("Ingredients:")
    for ingredient in ingredients:
        print(ingredient)

sandwitch('a', 'b', 'c', 'd')
sandwitch('asd', 'adf', 'afd')
sandwitch('banana', 'apple', 'strawberry')
result:

8-14

题目:
汽车:编写一个函数,将一辆汽车的信息存储在一个字典中。这个函数总是接受制造商和型号,还接受任意数量的关键字实参。这样调用这个函数:提供必不可少的信息,以及两个名称—值对,如颜色和选装配件。这个函数必须能够像下面这样进行调用:


code:
def make_car(brand, model, **features):
    car = {}
    car['brand'] = brand
    car['model'] = model
    for key, value in features.items():
        car[key] = value
    return car

car = make_car('subaru', 'outback', color='blue', tow_package=True)
print(car)
result:


8-15

题目:
打印模型 :将示例print_models.py中的函数放在另一个名为printing_functions.py的文件中;在print_models.py的开头编写一条import 语句,并修改这个文件以使用导入的函数。

code:


result:

猜你喜欢

转载自blog.csdn.net/zero_s_qiu/article/details/79781933