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

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值