第八章作业

"""8.1 消息"""
def display_message():
print("I'm learning to use function.")


display_message()




"""8.2 喜欢的图书"""
def favorite_book(title):
print("One of my favorite books is " + title.title() + ".")


favorite_book('Ordinary world')




"""8.3 T恤"""
def make_shirt(size, word):
print("The size of the T-shirt is: " + str(size))
print("The word to print in the T-shirt: " + str(word))


make_shirt('M', 'OK')
make_shirt(size = 'S', word = 'Yes')




"""8.4 大号T恤"""
def make_shirt(size, word = 'I love Python'):
print("The size of the T-shirt is: " + str(size))
print("The word to print in the T-shirt: " + str(word))


make_shirt('L')
make_shirt('M')
make_shirt(size = 'L', word = 'Yes')




"""8.5 城市"""
def describe_city(city, country = 'China'):
print(city.title() + " is in " + country.title() + ".")


describe_city('Guangzhou')
describe_city('Shanghai')
describe_city('Pairs', 'France')




"""8.6 城市名"""
def city_country(city, country):
print('"' + country.title() + ', ' + city.title() + '"')


city_country('shanghai', 'China')
city_country('guangzhou', 'China')
city_country('pairs', 'France')




"""8.7 专辑"""
def make_album(singer, album, total = ''):
if total:
return [singer, album, total]
else:
return [singer, album]


album = make_album('Sleep Alone', 'Eason')
print(album)
album = make_album('Xposed', 'Gloria Tang Tsz-Ke')
print(album)
album = make_album('Torches', 'Jason Zhang', 1)
print(album)




"""8.8 用户的专辑"""
def make_album(singer, album, total = ''):
if total:
return [singer, album, total]
else:
return [singer, album]


while 1:
i = input("Quik?1.Yes,2.No  ");
if i == '1':
break
singer = input("Please input the name of the singer:")
album = input("Please input the name of the album:")
sing = make_album(singer, album)
print(sing)




"""8.9 魔术师"""
def show_megicians(megicians):
for megician in megicians:
print(megician)


megicians = ['LiuQian', 'Yif', 'FuYandong']
show_megicians(megicians)




"""8.10 了不起的魔术师"""
def show_megicians(megicians):
for megician in megicians:
print(megician)


def make_great(megicians):
for i in range(0, len(megicians)):
megicians[i] = "the Great " + megicians[i]


megicians = ['LiuQian', 'Yif', 'FuYandong']
show_megicians(megicians)
print("\n", end = '')
make_great(megicians)
show_megicians(megicians)




"""8.11 不变的魔术师"""
def show_megicians(megicians):
for megician in megicians:
print(megician)


def make_great(megicians):
new_list = []
for megician in megicians:
megician = "the Great " + megician
new_list.append(megician)
return new_list


megicians = ['LiuQian', 'Yif', 'FuYandong']
new_list = make_great(megicians)
show_megicians(megicians)
print("\n", end = '')
show_megicians(new_list)




"""8.12 三明治"""
def make_sandwich(*ingredients):
print("Your sandwich:")
for ingredient in ingredients:
print(" " + ingredient)


make_sandwich('eggs', 'cheese')
make_sandwich('pineapple', 'mushrooms', 'green peppers')
make_sandwich('eggs')




"""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('Jia', 'Decheng',
status = 'Student',
country = 'China',
school = 'SYSU')
print(user_profile)




"""8.14 汽车"""
def make_car(manufacturer, model, **user_car):
car = {}
car['car_manufacturer'] = manufacturer
car['car_model'] = model
for key, value in user_car.items():
car[key] = value
return car


car = make_car('Bugatti', 'veyron',
color = 'white',
price = 'more than 2500Yuan',
highest_speed = '407km/h')
print(car)




"""8.15 打印模型"""
#printing_functions.py
def print_models(unprinted_designs, completed_models):
while unprinted_designs:
current_design = unprinted_designs.pop()

print("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)


#print_models.py
from printing_functions import *
unprint_designs = ['iphone case', 'robot pendant', 'dodecahedron']
completed_models = []
print_models(unprint_designs, completed_models)
show_completed_models(completed_models)

猜你喜欢

转载自blog.csdn.net/ad_jadson/article/details/79767239