1. 程式人生 > >《Python 程式設計:從入門到實踐》第八章(函式)練習題答案

《Python 程式設計:從入門到實踐》第八章(函式)練習題答案

# -*- coding: GBK -*-
#8-7
def make_album(singer,album,songsnum=''):
	sing_dict={'singer':singer,'album':album}
	if songsnum:
		sing_dict['songsnum']=songsnum
	return  sing_dict

print(make_album('sunyue','buzhid',10))	
#8-8
sing_list = []
continue_flag = True
while continue_flag:
	singer = input('please input singer name:')
	if singer!='q':
		album  = input('please input album name:')
		if album!='q':
			sing_list.append(make_album(singer,album))
		else:
			print(sing_list)
			continue_flag = False
	else:
		print(sing_list)
		continue_flag = False
#8-9
def show_magicians(magicians):
	for magician in magicians:
		print(magician.title())
magicians = ['alice', 'david', 'carolina']
show_magicians(magicians)

#8-10,11
def make_great(magicians,great_magicians):
	while magicians:
		magician = magicians.pop()
		magician = 'The Great '+ magician
		great_magicians.append(magician)

magicians = ['alice', 'david', 'carolina']
great_magicians=[]
make_great(magicians[:],great_magicians)
show_magicians(magicians)
#8-9
def show_magicians(magicians):
	for magician in magicians:
		print(magician.title())
magicians = ['alice', 'david', 'carolina']
show_magicians(magicians)

#8-10,11
def make_great(magicians,great_magicians):
	while magicians:
		magician = magicians.pop()
		magician = 'The Great '+ magician
		great_magicians.append(magician)

magicians = ['alice', 'david', 'carolina']
great_magicians=[]
make_great(magicians[:],great_magicians)
show_magicians(magicians)
#8-12
def add_material(*materials):
	for material in materials:
		print('you add',material)

add_material('1')
add_material('2','3','5')

#8-13
def build_profile(name,age,**other_info):
	print('your name is:',name)
	print('your age is:',age)
	for k,v in other_info.items():
		print ('your', k, 'is:',v)
build_profile('yawei',12,hometown='hb',hobby='reading')
#8-14
def make_car(factory,model,**other_infos):
	car_dict = {'factory':factory,'model':model}
	for k,v in other_infos.items():
		car_dict[k]=v
	return car_dict
	
car = make_car('subaru', 'outback', color='blue', tow_package=True)
print(car)
#8-16
#import sept8
from sept8 import make_car
#from sept8 import *
#from sept8 import make_car as mc
#import sept8 as s8
car = make_car('subaru', 'outback', color='blue', tow_package=True)
print(car)