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)