《Python编程 从入门到实践》第八章课后习题

本文通过几个示例介绍了Python中函数的基本定义与调用方法,包括默认参数、关键字参数及返回值等特性。

8-1

def display_message():
    print("we will learn function in this chapter. ")
	
display_message()

8-2

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

8-3

def make_shirt(size, words):
    print("Size: " + size +"\tWords: " + words)
make_shirt("XL", "PICK & ROLL")	
make_shirt(size = "XL", words = "PICK & ROLL")

8-4

def make_shirt(size = "L", words = "I love python"):
    print("Size: " + size +"\tWords: " + words)
	
make_shirt()
make_shirt("M")
make_shirt(words = "PICK & ROLL")

8-5

def describe_city(city = "Tokyo", country = "The Holy Empire of Britannia"):
    print(city + " is in " + country +". ")
	
describe_city()
describe_city("Pendragon")
describe_city("Luoyang", "Chinese Federation")

8-6

def city_country(city, country):
    print(city + ", " + country)
	
city_country("London", "EU")
city_country("Pendragon", "The Holy Empire of Britannia")
city_country("Luoyang", "Chinese Federation")

8-7

def make_album(singer_name, album_name):
    return {'singer': singer_name, 'album': album_name}

print(make_album("Jay Chou", "Fantasy"))
print(make_album("Eason", "Live For Today"))
print(make_album("Eason", "Shall We Dance? Shall We Talk!"))
def make_album(singer_name, album_name, num=''):
    return {'singer': singer_name, 'album': album_name, 'numbers': num}

print(make_album("Jay Chou", "Fantasy"))
print(make_album("Eason", "Live For Today"))
print(make_album("Eason", "Shall We Dance? Shall We Talk!", 10))

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值