def make_album(singer, album, songs = ''):
album_info = {'singer': singer, 'album': album}
if songs:
album_info['songs'] = songs
return album_info
print(make_album("Maksim","New Silk Road",7))
print(make_album("Tamas Wells","THE PLANTATION"))
print(make_album("Jackson Turner","Long Time Coming",5))
{'singer': 'Maksim', 'album': 'New Silk Road', 'songs': 7}
{'singer': 'Tamas Wells', 'album': 'THE PLANTATION'}
{'singer': 'Jackson Turner', 'album': 'Long Time Coming', 'songs': 5}
8-8 用户的专辑 :在为完成练习8-7编写的程序中,编写一个while 循环,让用户输入一个专辑的歌手和名称。获取这些信息后,使用它们来调用函数make_album() ,并将创建的字典打印出来。在这个while 循环中,务必要提供退出途径。
def make_album(singer, album, songs = ''):
album_info = {'singer': singer, 'album': album}
if songs:
album_info['songs'] = songs
return album_info
while True:
print("\nPlease tell me the singer's name.", end='')
print("(enter 'q' at any time to quit)")
singer = input("The singer's name: ")
if singer == 'q':
break
print("\nPlease tell me the album's name.", end='')
print("enter 'q' at any time to quit)")
album = input("The album's name: ")
if album == 'q':
break
album_info = make_album(singer, album)
message = "\nThe singer," + album_info['singer']
message += " 's album is " + album_info['album'] + "."
print(message)
Please tell me the singer's name.(enter 'q' at any time to quit)
The singer's name: Maksim
Please tell me the album's name.enter 'q' at any time to quit)
The album's name: New Silk Road
The singer,Maksim 's album is New Silk Road.
Please tell me the singer's name.(enter 'q' at any time to quit)
The singer's name: Jackson Turner
Please tell me the album's name.enter 'q' at any time to quit)
The album's name: Long Time Coming
The singer,Jackson Turner 's album is Long Time Coming.
Please tell me the singer's name.(enter 'q' at any time to quit)
The singer's name: q
8-10 了了不不起起的的魔魔术术师师 :在你为完成练习8-9而编写的程序中,编写一个名为make_great() 的函数,对魔术师列表进行修改,在每个魔术师的名字中都加入字样“the Great”。调用函数show_magicians() ,确认魔术师列表确实变了。
def show_magicians(magicians):
for magician in magicians:
print(magician)
def make_great(magicians):
for i in range(len(magicians)):
magicians[i] = "the Great " + magicians[i]
magicians = ["David Blaine", "David Copperfield", "Criss Angel"]
make_great(magicians)
show_magicians(magicians)
8-11 不不变变的的魔魔术术师师 :修改你为完成练习8-10而编写的程序,在调用函数make_great() 时,向它传递魔术师列表的副本。由于不想修改原始列表,请返回修改后的列表,并将其存储到另一个列表中。分别使用这两个列表来调用show_magicians() ,确认一个列表包含的是原来的魔术师名字,而另一个列表包含的是添加了字样“the Great”的魔术师名字。
def show_magicians(magicians):
for magician in magicians:
print(magician)
def make_great(magicians):
for i in range(len(magicians)):
magicians[i] = "the Great " + magicians[i]
return magicians
magicians = ["David Blaine", "David Copperfield", "Criss Angel"]
show_magicians(make_great(magicians[:]))
print()
show_magicians(magicians)
the Great David Blaine
the Great David Copperfield
the Great Criss Angel
David Blaine
David Copperfield
Criss Angel
8-12 三明治 :编写一个函数,它接受顾客要在三明治中添加的一系列食材。这个函数只有一个形参(它收集函数调用中提供的所有食材),并打印一条消息,对顾客点的三明治进行概述。调用这个函数三次,每次都提供不同数量的实参。
def make_sandwich(*ingredients):
print("\nThis sandwich is made of the following ingredients:")
for ingredient in ingredients:
print("- " + ingredient)
make_sandwich("vegetable salad")
make_sandwich("vegetable salad","pastrami")
make_sandwich("vegetable salad","pastrami","tuna")
This sandwich is made of the following ingredients:
- vegetable salad
This sandwich is made of the following ingredients:
- vegetable salad
- pastrami
This sandwich is made of the following ingredients:
- vegetable salad
- pastrami
- tuna
8-13 用户简介 :复制前面的程序user_profile.py,在其中调用build_profile() 来创建有关你的简介;调用这个函数时,指定你的名和姓,以及三个描述你的键-值对。
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('Wu','Yuezhou',location='China',age='21',gender='male')
print(user_profile)
{'first_name': 'Wu', 'last_name': 'Yuezhou', 'location': 'China', 'age': '21', 'gender': 'male'}
8-14 汽车 :编写一个函数,将一辆汽车的信息存储在一个字典中。这个函数总是接受制造商和型号,还接受任意数量的关键字实参。这样调用这个函数:提供必不可少的信息,以及两个名称—值对,如颜色和选装配件。
def make_car(manufacturer, model, **cars_info):
car = {}
car['manufacturer'] = manufacturer
car['model'] = model
for key,value in cars_info.items():
car[key] = value;
return car;
car = make_car('subaru', 'outback', color='blue', tow_package=True)
for key,value in car.items():
print("The car's " + key + " is " + str(value) + ".")
The car's manufacturer is subaru.
The car's model is outback.
The car's color is blue.
The car's tow_package is True.