8-1
def display_message(profession):
print('我的大学专业是'+profession)
display_message('电科')
我的大学专业是电科
8-2
def favorite_book(title):
print('One of my favorite books is '+title+'.')
favorite_book('Flutter')
One of my favorite books is Flutter.
8-3
def make_shirt(size,pattern):
print('The size is '+size+'.')
print('The pattern is '+pattern+'.')
make_shirt('XL','China')
The size is XL.
The pattern is China.
8-4
def make_shirt(size,pattern='I love Python'):
print('The size is "'+size+'".')
print('The pattern is "'+pattern+'".')
make_shirt('big')
make_shirt('medium')
make_shirt('big','Chine')
The size is "big".
The pattern is "I love Python".
The size is "medium".
The pattern is "I love Python".
The size is "big".
The pattern is "Chine".
8-5
def describe_city(city,country='US'):
print(city.title()+' is in '+country+'.')
describe_city('London','UK')
describe_city('New York')
describe_city('Los Angeles')
London is in UK.
New York is in US.
Los Angeles is in US.
8-6
def city_country(city,country):
return (city+','+country)
print(city_country('London','UK'))
print(city_country('New York','US'))
print(city_country('Los Angeles','US'))
London,UK
New York,US
Los Angeles,US
8-7
def make_album(name,album,number=' '):
albums={}
albums[name]=album
if number != ' ':
albums['number']=number
return albums
print(make_album('A','a'))
print(make_album('B','b','5'))
print(make_album('C','c'))
{'A': 'a'}
{'B': 'b', 'number': '5'}
{'C': 'c'}
8-8
def make_album(name,album,number=' '):
albums={}
albums[name]=album
if number != ' ':
albums['number']=number
return albums
while True:
name=input('请输入歌手名:')
if name != 'quit':
album=input('请输入专辑名:')
print(make_album(name,album))
else:
break
请输入歌手名:A
请输入专辑名:a
{'A': 'a'}
请输入歌手名:quit
8-9
names=['A','B','C']
def show_magicians(names):
for name in names:
print(name)
show_magicians(names)
A
B
C
8-10
names=['A','B','C']
new_names=[]
def make_great(names):
for name in names:
name='The Great '+name
new_names.append(name)
return new_names
def show_magicians(new_names):
for name in new_names:
print(name)
a=make_great(names)
show_magicians(a)
The Great A
The Great B
The Great C
8-11
names=['A','B','C']
new_names=[]
def make_great(names):
for name in names:
name='The Great '+name
new_names.append(name)
return new_names
def show_magicians(names):
for name in names:
print(name)
a=make_great(names[:])
show_magicians(a)
show_magicians(names)
The Great A
The Great B
The Great C
A
B
C
8-12
def make_sandwich(*food_material):
print(food_material)
make_sandwich('salt')
make_sandwich('egg','bread')
make_sandwich('egg','lettuce','salt')
('salt',)
('egg', 'bread')
('egg', 'lettuce', 'salt')
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('张','三',身高='180cm',体重='70kg')
print(user_profile)
{'first_name': '张', 'last_name': '三', '身高': '180cm', '体重': '70kg'}
8-14
def car_profile(manufacturer,model,**message):
profile={}
profile['manufacturer']=manufacturer
profile['model']=model
for key,value in message.items():
profile[key]=value
return profile
car=car_profile('subaru','outback',color='blue',tow_package=True)
print(car)
{'manufacturer': 'subaru', 'model': 'outback', 'color': 'blue', 'tow_package': True}