#练习 8-1:
def display_message():
"""指出我在此章学的是什么"""
print('我在本章学的是函数')
display_message()
#练习 8-2:
def favorite_book(title):
print(f'One of my favorite book is {title.title()}')
favorite_book('the great gatsby')
#练习 8-3:
def make_shirt(size,typeface):
print(f"T_shirt's size is {size.title()}")
print(f"The words to be printed on the T-shirt is ‘{typeface.upper()}’")
make_shirt('L', 'I LOVE YOU')
make_shirt(typeface = 'peace and love',size = 'm')
# #练习 8-4:
def make_shirt(size = 'l',typeface = 'I LOVE python'):
print(f"T_shirt's size is {size.upper()}")
print(f"The words to be printed on the T-shirt is ‘{typeface.title()}’")
make_shirt()
make_shirt('m')
make_shirt(typeface = 'I prefer you,my darling!')
#练习 8-5:
def describe_city(city,country = 'china'):
print(f"{city.title()} is in {country.title()}.")
describe_city('changsha')
describe_city('zhuzhou')
describe_city('london',country = 'britain')
#练习 8-6:
def city_country(city,country):
return f"{city.title()},{country.title()}"
print(city_country('changsha','china'))
print(city_country('paris','france'))
print(city_country('london','britain'))
#练习 8-7:
def make_album(singer_name,album_name,Number_songs = None):
albums = {'name':singer_name,'album':album_name}
if Number_songs:
albums['number'] = Number_songs
return albums
print(make_album('周杰伦','《依然范特西》',10))
print(make_album('王力宏','《盖世英雄》',10))
print(make_album('陈奕迅','《L.O.V.E.》'))
#练习 8-8:
def make_album(singer_name,album_name,Number_songs = None):
albums = {'name':singer_name,'album':album_name}
#返回含有名字和专辑的字典,专辑内歌曲数可选
if Number_songs:
albums['number'] = Number_songs
return albums
inf = 'wlecome to this Song selection software!\n'
inf += "let's start it from now!\n"
inf += "(enter q at any time to quit)"
inf_1 = 'one last question\n'
inf_1 += 'do you khow how many songs in this album(yes/no)\n'
inf_2 = 'Anyone else want to try?(yes/no)\n'
sin_name = alb_name = fact = alb_number = act ='1'
while True:
print(inf)
sin_name = input('Please input singer name:')
if sin_name == 'q':
break
alb_name = input('Please input one of his album name:')
if sin_name == 'q':
break
fact = input(inf_1)
if sin_name == 'q':
break
if fact == 'no':
print(make_album(sin_name,alb_name))
break
#如果不知道专辑歌曲数目可以选择退出,生成只有歌手名和专辑名的字典
alb_number = input('Please input the number of songs:')
if sin_name == 'q':
break
print(make_album(sin_name,alb_name,alb_number))
act = input(inf_2)
if sin_name == 'q':
break
if act == 'no':
break
#可以选择对下一个人进行调查
#练习 8-9:
Arcane = ['because you are jinx','We are alone together']
def show_messages():
for message in Arcane:
print(message)
show_messages()
#练习 8-10:
def send_messages(dialogue,complete_message):
#用来传递列表间的信息
while dialogue:
current_message = dialogue.pop()
print(f'sending_message:{current_message}')
complete_message.append(current_message)
def show_messages(ava_message):
#用于展示列表信息
if ava_message:
print('该列表有以下信息')
for message in ava_message:
print(message)
else:
print('\n该列表没有任何信息\n')
Arcane_dialogue = ['because you are jinx','We are alone together']
complete_message = []
send_messages(Arcane_dialogue,complete_message)
show_messages(Arcane_dialogue)
show_messages(complete_message)
#练习 8-11:
def send_messages(dialogue,complete_message):
#用来传递列表间的信息
while dialogue:
current_message = dialogue.pop()
print(f'sending_message:{current_message}')
complete_message.append(current_message)
def show_messages(ava_message):
#用于展示列表信息
if ava_message:
print('该列表有以下信息')
for message in ava_message:
print(message)
else:
print('\n该列表没有任何信息\n')
Arcane_dialogue = ['because you are jinx','We are alone together']
complete_message = []
send_messages(Arcane_dialogue[:],complete_message)
show_messages(Arcane_dialogue)
show_messages(complete_message)
#练习 8-12:
def make_sandwiches(*toppings):
#打印顾客要点的配料
list(toppings)
m = (','.join(toppings))
print(f'your sandwich includes {m}.')
make_sandwiches('coco','milk')
make_sandwiches('ice')
make_sandwiches('tea','fire','water')
#练习 8-13:
def build_profile(first,last,**user_info):
#创建一个字典,其中包含我们知道的有关用户的一切
user_info['first_name'] = first
user_info['last_name'] = last
return user_info
user_profile = build_profile('jie','luo',
height='173cm',
weight='62kg',
outward='handsome')
print(user_profile)
#练习 8-14:
def make_car(manufacturer,model,**more_info):
more_info['manufacturer_name'] = manufacturer
more_info['model_name'] = model
return more_info
car = make_car('benz', 'Maybach', colour='black' , tow_package=True)
print(car)
#练习 8-15:
#以下内容保存至printing_functions.py
def print_models(unprinted_designs, completed_models):
"""
模拟打印每个设计,直到没有未打印的设计为止。
打印每个设计后,都将其移到列表 completed_models 中。
"""
while unprinted_designs:
current_design = unprinted_designs.pop()
# 模拟根据设计制作 3D 打印模型的过程。
print(f"Printing model: {current_design}")
completed_models.append(current_design)
def show_completed_models(completed_models):
"""显示打印好的所有模型。"""
print("\nThe following models have been printed:")
for completed_model in completed_models:
print(completed_model)
#######################################################
#在printing_models.py中打开并调用printing_functions
import printing_functions
unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
completed_models = []
printing_functions.print_models(unprinted_designs, completed_models)
printing_functions.show_completed_models(completed_models)
#练习 8-16:
#yes
#练习 8-17:
#yes
第8章--函数
最新推荐文章于 2024-07-26 21:19:18 发布