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

这个博客通过一系列的示例介绍了Python编程中的函数应用,包括显示消息、定义带有默认参数的函数、创建字典表示专辑、模拟打印魔术师名字、接受任意参数的三明治函数,以及用户简介和汽车信息的构建等。还涵盖了导入模块的不同方式。
摘要由CSDN通过智能技术生成

coding=gbk

coding:utf-8!

8-1消息

def display_message():
“”“显示简单的语句”""
print(“We will learn function from this chapter.”)
display_message()

8-2喜欢的图书

print()
def favorite_book(title): # 注意这里形参不需要加单引号
“”“显示喜欢的图书”""
print("One of my favorite books is " + title + “.”)
favorite_book(‘Alice in Wonderland’) # 注意括号里面的实参要加单引号

8-3 T恤

def make_shirt(size, word):
“”“显示T恤尺码及字样”""
print("\nThe size is " + size + “.”) # 可以在size前面加str()
print("The word on the T-shirt is " + word + “.”)
make_shirt(‘S’, ‘Hello World’)

8-4大号T恤

def make_shirt(size = ‘L’, word = ‘I love Python’):
“”“显示默认‘我爱Python字样’的大号T恤”""
print("\nThe size is " + size + “.”)
print("The word on the T-shirt is " + word + “.”)
make_shirt()
make_shirt(size = ‘M’)
make_shirt(size = ‘S’, word = ‘Hello world’)

8-5城市

def describe_city(city_name, nation = ‘China’):
“”“城市名字及所属国家”""
print("\n" + city_name + " is in " + nation + “.”)
describe_city(‘Beijing’)
describe_city(‘Shenzhen’)
describe_city(‘London’, ‘England’)

8-6城市名

print()
def city_country(city, country):
“”“返回城市及所属国家”""
str = city + ", " + country
return str.title()
number1 = city_country(‘beijing’, ‘china’)
number2 = city_country(‘shenzhen’, ‘china’)
number3 = city_country(‘london’, ‘england’)
print(number1)
print(number2)
print(number3)

8-7

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值