8-1消息
def desplay_message():
print ("I am learning function!")
desplay_message()
8-2喜欢的图书
def favorite_book(title):
print ("One of my favorite book is " + title)
favorite_book("Alice in Wonderland")
8-3T恤
def make_shirt(size ,name):
print ("The size is:" + size +",and the name is " + name)
make_shirt("XL","I am happy")
8-4大号T恤
def make_shirt(size = 'L' ,name = "I love python"):
print ("The size is:" + size +",and the name is " + name)
make_shirt(size = 'L')
make_shirt(size = 'M')
make_shirt("XL","I am happy")
8-5城市
def deccribe_city(city = 'Los angelos',nation = 'the USA'):
print (city + " is in " + nation)
deccribe_city(city = 'New York')
deccribe_city(city = 'Washington')
deccribe_city(city = 'London', nation='the UK')
8-6城市名
def city_country(city ,country):
name=city + ','+country
return name
print(city_country('New York','the USA'))
print(city_country('London','the UK'))
print(city_country('Paris','France'))
8-7专辑
def make_album(siname,alname):
return {'singer':siname , 'album':alname}
print(make_album('Jay','Ye Huimei'))
print(make_album('JJ','New earth'))
print(make_album('Jolin','Beauty Butterfly'))
8-8用户的专辑
def make_album(siname,alname):
return {'singer':siname , 'album':alname}
while True:
siname = input ('Please input the name of the singer:')
if siname == 'q':
break
alname = input ('Please input the name of the album:')
if alname == 'q':
break
print (make_album(siname,alname))
8-9魔术师
def show_migicians(magic):
for name in magic:
print(name)
magic = ['David' , 'Bob' , 'Kevin']
show_migicians(magic)
8-10了不起的魔术师
def show_migicians(magic):
for name in magic:
print(name)
def new(magic):
lenth = len(magic)
for i in range(0,lenth):
magic[i] = 'the great ' + magic [i]
magic = ['David' , 'Bob' , 'Kevin']
new(magic)
show_migicians(magic)
8-11不变的魔术师
def show_migicians(magic):
for name in magic:
print(name)
def new(magic):
lenth = len(magic)
for i in range(0,lenth):
magic[i] = 'the great ' + magic [i]
magic = ['David' , 'Bob' , 'Kevin']
magic1 = []
for name in magic:
magic1.append(name)
new(magic1)
show_migicians(magic)
show_migicians(magic1)
8-12三明治
def sandwich(*thing):
print('The sandwich was added by ' , thing)
sandwich('beef','pork')
sandwich('vegatable','lettethe')
sandwich('onions','cheese')
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
my_profile = build_profile('Lucy','Zheng',location='Guangzhou',field = 'Computer Science')
print(my_profile)
8-14汽车
def car(producer,ID,**thing):
acar={}
acar['producer']=producer
acar['ID']=ID
for key,value in thing.items():
acar[key]= value
return acar
carfile = car('Bob','10000',owner = 'LucyZheng')
print(carfile)
8-15 打印模型
import print_functions
unprinted_designs = ['iphone case','robot pendat','dodecehedron']
completed_models = print_functions.miao(unprinted_designs)
print ('\nThe following models have been printed:')
for completed_model in completed_models:
print(completed_model)
#print_functions.py如下:
def miao(unprinted_designs):
completed_models=[]
while unprinted_designs:
current_design = unprinted_designs.pop()
print('Printing model: ' + current_design)
completed_models.append(current_design)
return completed_models
8-16
8-15中的程序中import改为:
from print_functions import miao from print_functions import miao as m from print_functions as prints from print_functions import *