8-1
def display_message():
print('We learn about function in this chapter')
display_message()
输出:
We learn about function in this chapter
8-2
def favorite_book(title):
print('One of my favorite book is '+title)
favorite_book('Alice in Wonderland')
输入:
Alice in Wonderland
输出:
One of my favorite book is Alice in Wonderland
8-3
def make_shirt(size,name='I love Python'):
print('We make a '+size+' shirt print with '+name)
make_shirt('large')
make_shirt('middle')
make_shirt('middle','I hate C++')
输出:
We make a large shirt print with I love Python
We make a middle shirt print with I love Python
We make a middle shirt print with I hate C++
8-5
def describe_city(city,country='China'):
print(city+' is in '+country)
describe_city('Guangzhou')
describe_city('Shanghai')
describe_city('New York','America')
输出:
Guangzhou is in China
Shanghai is in China
New York is in America
8-7
def make_album(names,albums,numbers=['','','']):
for number in numbers:
if number:
album_one={'name':names.pop(),'album_name':albums.pop(),'number':number}
else:
album_one={'name':names.pop(),'album_name':albums.pop()}
print(album_one)
names=['Mike','Tina','Tom']
album=['sky','love','black']
numbers=['','3','4']
make_album(names,album,numbers)
输出:
{'name': 'Tom', 'album_name': 'black'}
{'name': 'Tina', 'album_name': 'love', 'number': '3'}
{'name': 'Mike', 'album_name': 'sky', 'number': '4'}
8-12
def make_sandwish(*adds):
print('You want ',end='')
for add in adds:
print(add+' ',end='')
print()
make_sandwish('cheese','beef')
make_sandwish('beef')
make_sandwish('ham','chicken','cheese')
输出:
You want cheese beef
You want beef
You want ham chicken cheese