P116作业
#8-1
def display_message():
print('在本章学习函数')
display_message()
#8-2
print('')
def favorite_book(title):
print('One of my favorite book is '+title.title()+'.')
favorite_book('alice in wonderland')
P121作业
#8-3
def make_shirt(size,type):
print('这件衬衫尺码是'+size.title()+',图案是'+type.title()+'。')
#位置实参
make_shirt('s','可乐')
#关键字实参
make_shirt(size = 's',type = '可乐')
#8-4
def make_shirt(size,type = 'I love Python'):
print('这件衬衫尺码是'+size.title()+',图案是'+type.title()+'。')
make_shirt('l')
make_shirt('m')
make_shirt('s',type = 'ok')
#8-5
def describe_city(city,country = 'China'):
print(city.title()+' is in '+country+'.')
describe_city('changchun')
describe_city('tokyo',country = 'Japan')
P125作业
#8-6
def city_country(city,country):
message = city+','+country
return message.title()
word = city_country('santiago','chile')
print(word)
#8-7
def make_album(singer,album,number=''):
if number:
person = {'singer':singer,'album':album,'number':number}
return person
person = {'singer':singer,'album':album}
return person
a = make_album('周杰伦','叶惠美')
b = make_album('许嵩','清明雨上')
print(a)
print(b)
c = make_album('许嵩','清明雨上',3)
print(c)
#8-8
def make_album(singer,album):
person = {'singer':singer,'album':album}
return person
while True:
print("(enter 'q' at any time to quit)")
a = input('Please input a singer:')
if a == 'q':
break
b = input("Please input the singer's album:")
if b == 'q':
break
c = make_album(a,b)
print(c)
P129作业
#8-9
def show_magicians(magicians):
for magician in magicians:
print(magician)
magicians = ['a','b','c','d']
show_magicians(magicians)
#8-10
def show_magicians(magicians):
while magicians:
make_great(magicians)
break
def make_great(magicians):
for magician in magicians:
print('the great '+magician)
magicians = ['a','b','c','d']
show_magicians(magicians)
#8-11
def show_magicians(magicians):
while magicians:
make_great(magicians)
break
def make_great(magicians):
for magician in magicians:
print('the great '+magician)
magicians = ['a','b','c','d']
show_magicians(magicians)
print(magicians)
P132作业
#8-12
def sandwich(*foods):
print('顾客点了这些三明治:')
for food in foods:
print(food)
sandwich('a')
sandwich('a','b','c')
sandwich('a','dfadf','adfe','adfeefd')
#8-13
print('')
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('晨','谢',年龄 = '23', 性别 = '女', 家乡 = '长春')
print(user_profile)
#8-14
print('')
def make_car(manufacturer,model,**car_info):
profile = {}
profile['manufacturer'] = manufacturer
profile['model'] = model
for key,value in car_info.items():
profile[key] = value
return profile
car_profile = make_car('sabaru','outback',color = 'blue',tow_package = True)
print(car_profile)
P137作业
模块 printing_functions.py
def print_models(unprinted_designs, 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)
模块 magician.py
def show_magicians(magicians):
for magician in magicians:
print(magician)
P137作业+调用模块的五种方式:
#8-15
import printing_functions
unprinted_designs = ['iphone case','robot pendant','dodecahedron']
completed_models = []
printing_functions.print_models(unprinted_designs,completed_models)
printing_functions.show_completed_models(completed_models)
#8-16
print('')
import magician
magicians = ['ab','cd','ef','gh']
magician.show_magicians(magicians)
print('')
from magician import show_magicians
magicians = ['ab','cd','ef','gh']
show_magicians(magicians)
print('')
from magician import show_magicians as mg
magicians = ['ab','cd','ef','gh']
mg(magicians)
print('')
import magician as m
magicians = ['ab','cd','ef','gh']
m.show_magicians(magicians)
print('')
from magician import *
magicians = ['ab','cd','ef','gh']
show_magicians(magicians)
不足之处望多加指正。