Python编程:从入门到实践(课后习题8)

# 8-1 消息
def display_message():
	print("This chapter I will learning function.")

display_message()

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

favorite_book('Python for Data Analysis')

# 8-3 T恤
def make_shirt(size, word):
	print("T-shirt's size is: " + str(size) + '.')
	print("T-shirt's word is: " + str(word) + '.')

make_shirt(L, 24)
make_shirt(word='I love you', size='M')

# 8-4 大号T恤
def make_shirt(size='大号', word='I love Python.'):
	print("T-shirt's size is: " + str(size) + '.')
	print("T-shirt's word is: " + str(word) + '.')

make_shirt()
make_shirt(size='中号')
make_shirt(size='加大号', word="I don't care")

# 8-5 城市
def describe_city(city, nation='China'):
	print("City: " + city)
	print("Nation: " + nation)

describe_city('Guangzhou')
describe_city('Beijing')
describe_city('Los angels', nation='America')

# 8-6 城市名
def city_country(city, nation):
	x = city + ', ' + nation
	return x

city_nation1 = city_country('Guangzhou', 'China')
city_nation2 = city_country('Tokyo', 'Japan')
city_nation3 = city_country('Seoul', 'Korea')
print(city_nation1)
print(city_nation2)
print(city_nation3)

# 8-7 专辑
def make_album(singer, album, num=''):
	albums = {}
	if num:
		albums['singer'] = singer
		albums['album'] = album
		albums['num'] = num
	else:
		albums['singer'] = singer
		albums['album'] = album
	return albums

u87 = make_album('陈奕迅', 'U87')
u87_num = make_album('陈奕迅', 'U87', num=12)
happy = make_album('陈奕迅', '我的快乐时代', num=15)
print(u87)
print(u87_num)
print(happy)

# 8-8 用户的专辑
album_active = True
while album_active:
	singer = input("Singer: ")
	album = input("Album: ")
	num = input("Number: ")
	you_album = make_album(singer, album, num)
	print(you_album)

	quit = input("Continue or quit? (y/n) ")
	if quit == 'n':
		album_active = False

# 8-9 魔术师
magician = ['criss', 'jason', 'cyril']
def show_magicians(magician_list):
	for magician in magician_list:
		print(magician.title())

show_magicians(magician)

# 8-10 了不起的魔术师
def make_great(magician_list):
	for i in range(3):
		magician_list[i] = 'the Great ' + magician_list[i]
	return magician_list  # 差点忘记return了

make_great(magician)
show_magicians(magician)

# 8-11 不变的魔术师
magician_great = make_great(magician[:])
show_magicians(magician)
show_magicians(magician_great)

# 8-12 三明治
def sandwich(*args):
	for i in args:
		print(i)

# 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

my_profile = build_profile('Zhou', 'Kai', user_info={'age': 26, 'hobby': 'basketball', 'lover': 'Zhanglili'})
print(my_profile)

# 8-14 汽车
def car_information(maker, type1, **kw):
	information = {}
	information['maker'] = maker
	information['type'] = type1
	for i, j in kw.items():
		information[i] = j
	return information

    原文作者:周小馬
    原文地址: https://blog.csdn.net/z120379372/article/details/78027710
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞