python编程:从入门到实践 第八章课后题

本文通过具体实例介绍了Python函数的定义与使用方法,包括不同参数类型的传递方式、返回值、灵活运用关键字参数等技巧。

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

python编程:从入门到实践 第八章课后题

8-1:消息
编写一个名为display_message() 的函数,它打印一个句子,指出你在本章学的是什么。调用这个函数,确认显示的消息正确无误。
def display_message():
    print("I will learn the usage of function in this chapter!")

display_message()
In [1]: I will learn the usage of function in this chapter!
8-3
def make_shirt(size, font):
    print("This T-shirt is " + str(size) + " big, the '" + font + "' is on it")

make_shirt(32,"hello")
make_shirt(size = 32, font = "hello")
In [2]: runfile(...)
This T-shirt is 32 big, the 'hello' is on it
This T-shirt is 32 big, the 'hello' is on it
8-6
def city_country(city,country):
    return str(city.title() + ', ' + country.title())

cc = [('beijing','China'),('New York','American'),('St.Petersburg','Russia')]
for city,country in cc:
    print(city_country(city,country))
In [3]: runfile(...)
Beijing, China
New York, American
St.Petersburg, Russia
8-9
def show_magicians(megicians):
    for me in megicians:
        print(me)

megicians = ['wuda',"wuer","wusan"]
show_magicians(megicians)
In [4]: runfile(...)
wuda
wuer
wusan
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('huang', 'dirun',
                             age=20,
                             field='cs',
                             favorite='sports'
                             )
print(user_profile)
In [5]: runfile(...)
{'first_name': 'huang', 'last_name': 'dirun', 'age': 20, 'field': 'cs', 'favorite': 'sports'}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值