教材第八章习题

本文介绍了通过Python函数实现信息展示、个性化定制T恤、城市描述及音乐专辑信息汇总等实用功能。利用不同函数传递参数的方式,展示了灵活编程的魅力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

8-1 消息

def display_message():
	print("I learn fuctions")

display_message()

输出

I learn fuctions

8-2 喜欢的图书

def favorite_book(title):
	print("One of my favorite books is " + title + ".")

favorite_book("Alice in Wonderland")

输出

One of my favorite books is Alice in Wonderland.

8-3 T恤

def make_shirt(size, words):
	print("The T-shrt is " + size +" size and the words on it are " + words )

make_shirt("large", "Love Jin")
make_shirt(words = "Love Jin", size = "large")

输出

The T-shrt is large size and the words on it are Love Jin
The T-shrt is large size and the words on it are Love Jin

8-4 大号T恤

def make_shirt(size = "large", words = "I Love Python"):
	print("The T-shrt is " + size +" size and the words on it are " + words )

make_shirt()
make_shirt(size = "middle")
make_shirt(size = "small", words = "Love Jin")

输出

The T-shrt is large size and the words on it are I Love Python
The T-shrt is middle size and the words on it are I Love Python
The T-shrt is small size and the words on it are Love Jin
8-5 城市
def describe_city(name, country = "China"):
	print(name + "is in " + country)

describe_city("Reykjavik", "Iceland")
describe_city("Kunming")
describe_city("NewYork", "America")

输出

Reykjavikis in Iceland
Kunmingis in China
NewYorkis in America

8-6 城市名

def describe_city(name, country = "China"):
	return(name + ", " + country)

print(describe_city("Reykjavik", "Iceland"))
print(describe_city("Kunming"))
print(describe_city("NewYork", "America"))

输出

Reykjavik, Iceland
Kunming, China
NewYork, America

8-7 专辑

def make_album(singer, album, number = ''):
	message = {'singer': singer, 'album': album}
	if number:
		message['number'] = number
	return(message)


print(make_album("Jay Zhou", "12 new works"))
print(make_album("MayDay", "BuBu"))
print(make_album("XuSong", "Half City", 12))

输出

{'singer': 'Jay Zhou', 'album': '12 new works'}
{'singer': 'MayDay', 'album': 'BuBu'}
{'singer': 'XuSong', 'album': 'Half City', 'number': 12}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值