def display_message():
print('本章学习的函数')
display_message()
def favorite_book(title):
print('One of my favorite books is ' + title.title())
favorite_book('python')
def make_shirt(size,pattern):
print('The size is ' + size + '\tThe pattern is ' + pattern)
make_shirt('L','cat')
def make_shirt(size,pattern = 'I love Python'):
print('The size is ' + size + '\tThe pattern is ' + pattern)
make_shirt('XL')
make_shirt('L')
make_shirt('XL','cat')
def describe_city(city,country='China'):
print(city.title() + ' is in ' + country.title())
describe_city('beijing')
describe_city('qingdao')
describe_city('new york','USA')
def city_country(city,country):
message = city.title() + ',' + country.title()
return message
print(city_country('beijing','china'))
def make_album(name,album_name,number = ''):
if number:
album = {'name':name,'album_name':album_name,'number':number}
else:
album = {'name':name,'album_name':album_name}
return album
print(make_album('周杰伦','听妈妈的话'))
print(make_album('某某','某某某','5'))
def make_album(name,album_name,number=''):
album = {'name':name,'album_name':album_name}
return album
while True:
print("(enter 'q' at any time to quit)")
name = input('请输入歌手名:')
if name == 'q':
break
album_name = input('请输入专辑名:')
if album_name == 'q':
break
print(make_album(name,album_name))
magicians = ['ergou','qiqi','shitou']
def show_magicianslis(magicians):
for name in magicians:
print(name)
show_magicianslis(magicians)
magicians = ['ergou','qiqi','shitou']
great_magicians = []
def make_great(magicians,great_magicians):
while magicians:
magician = magicians.pop()
great_magician = 'the Great ' + magician
great_magicians.append(great_magician)
def show_magicianslis(great_magicians):
for name in great_magicians:
print(name)
make_great(magicians,great_magicians)
show_magicianslis(great_magicians)
magicians = ['ergou','qiqi','shitou']
great_magicians = []
def make_great(magicians,great_magicians):
while magicians:
magician = magicians.pop()
great_magician = 'the Great ' + magician
great_magicians.append(great_magician)
def show_magicianslis(great_magicians):
for name in great_magicians:
print(name)
make_great(magicians[:],great_magicians)
show_magicianslis(great_magicians)
show_magicianslis(magicians)
def sandwich(*toppings):
print("\nMaking a sandwich with the following toppings:")
for topping in toppings:
print("- " + topping)
sandwich('apple')
sandwich('banana','apple')
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('albert','einstein',location='princeton',field='physics',)
print(user_profile)
def make_car(manufacturer, model, **type_info):
"""创建一个字典,其中包含我们知道的有关用户的一切"""
profile = {}
profile['manufacturer'] = manufacturer
profile['model'] = model
for key, value in type_info.items():
profile[key] = value
return profile
car = make_car('subaru', 'outback', color='blue', tow_package=True)
print(car)
def print_models(unprinted_designs, completed_models):
"""
模拟打印每个设计,直到没有未打印的设计为止
打印每个设计后,都将其移到列表completed_models中
"""
while unprinted_designs:
current_design = unprinted_designs.pop()
print("Printing model: " + current_design)
completed_models.append(current_design)
def show_completed_models(completed_models):
"""显示打印好的所有模型"""
print("\nThe following models have been printed:")
for completed_model in completed_models:
print(completed_model)
----------------------------------------------
from printing_functions import *
unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
completed_models = []
print_models(unprinted_designs, completed_models)
show_completed_models(completed_models)
def display_message():
print('本章学习的函数')
----------------------------------------------
import displaying
displaying.display_message()
from displaying import display_message
display_message()
from displaying import display_message as dm
dm()
import displaying as dm
dm.display_message()
from displaying import *
display_message()
#8-17 函数编写指南
略