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}