【Week4.2】第八章(函数)

#8-7不太理解为什么打印出来的字典的键值对顺序和创建时的顺序不同,singer和album之间的顺序有点奇怪;
#8-8:这一题由于缩进错误,其实是逻辑上的问题,导致一开始没能顺利打印出正确的结果;
#8-15使用import的时候感觉命名很重要,可能因为命名原因,而无法正确运行;

#8-1
def dispaly_message():
	print("The usage of function in python.")
dispaly_message()


#8-2
def favorite_book(title):
	print("One of my favorite book is " + title + ".")
favorite_book("Alice in Wonderland")


#8-3
def make_shirt(size, logo):
	print("The size of the shirt is " + size + ".")
	print("And i want '" + logo + "' on it.")
make_shirt("L", "Fighting!")
make_shirt(size='M', logo='pretty!')



#8-4
def make_shirt(size='L', logo='I Love Python'):
	print("The size of the shirt is " + size + ".")
	print("And i want '" + logo + "' on it.")
make_shirt()
make_shirt(size='M')
make_shirt(logo='Fun!')

#8-5
def describe_city(city='Reykjavik', country='Iceland'):
	print(city + ' is in ' + country + '.')
describe_city()
describe_city('cityA')
describe_city(city='cityB')
describe_city(city='cityc', country='countryC')


#8-6
def city_country(city, country):
	print('"' + city + ', ' + country + '"')
city_country('A1', 'A2')
city_country('B1', 'B2')
city_country('C1', 'C2')


#8-7
def make_album(singer_name, album_name):
	albums = {'singer': singer_name, 'album':album_name}
	return albums
A1 = make_album('Justin Bieber', 'My World')
B2 = make_album('JB', 'christmas')
print(A1)
print(B2)

def make_album(singer_name, album_name, nums=""):
	albums = {'singer': singer_name, 'album':album_name}
	if nums:
		albums['nums'] = nums 
	return albums
A1 = make_album('Justin Bieber', 'My World')
B2 = make_album('JB', 'christmas')
C3 = make_album('JB', 'sorry', 2)
print(A1)
print(B2)
print(C3)

#8-8
def make_album(singer_name, album_name):
	albums = {'singer': singer_name, 'album':album_name}
	return albums
	
while True:
	print("\nPlease input a singer name:")
	print("(enter 'q' at any time to quit)")
	singer = raw_input("singer: ")
	if singer == 'q':
		break
	album = raw_input("album: ")
	if album== 'q':
		break;
	A = make_album(singer, album)
	print(A)


#8-9
def show_magicians(names):
	for name in names:
		print(name.title())
magician_name = ['alice', 'betty', 'cindy']
name = show_magicians(magician_name)

#8-10
magician_name = ['alice', 'betty', 'cindy']
great_name = []
def show_magicians(names):
	for name in names:
		print(name.title())
def make_great(magician_name):
	while magician_name:
		current_name = "the Great " + magician_name.pop()
		great_name.append(current_name)
make_great(magician_name)
show_magicians(great_name)


#8-11
magician_name = ['alice', 'betty', 'cindy']
great_name = []
def show_magicians(names):
	for name in names:
		print(name.title())
def make_great(magician_name):
	while magician_name:
		current_name = "the Great " + magician_name.pop()
		great_name.append(current_name)
make_great(magician_name[:])

show_magicians(great_name)
show_magicians(magician_name)


#8-12
def sandwiches(*materials):
	print("This customer want the materials below:")
	for material in materials:
		print("-" + material)
sandwiches('apple', 'banana', 'cabbage')
sandwiches('toufu', 'fish')




#8-14
def make_car(maker, model, **other_info):
	the_car = {}
	the_car['makers'] = maker
	the_car['models'] = model
	for key, value in other_info.items():
		the_car[key] = value
	return the_car
car = make_car('sakuru', 'outback', color='blue', tow_package=True)
print(car)



#printing_functions
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(models):
	print("\nThe following models have been printed:")
	for completed_model in models:
		print(completed_model)

直接导入文件:

#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)

导入文件中的函数:

from printing_functions import print_models, show_completed_models
unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
completed_models = []
print_models(unprinted_designs, completed_models)
show_completed_models(completed_models)

导入文件中函数模块并重新命名:这里多个模块一起命名会出错,也许只有分开命名才行!

from printing_functions import print_models as pm
from printing_functions import show_completed_models as scm
unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
completed_models = []
pm(unprinted_designs, completed_models)
scm(completed_models)

导入文件并重新命名:

import printing_functions as pf
unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
completed_models = []
pf.print_models(unprinted_designs, completed_models)
pf.show_completed_models(completed_models)

导入文件所有的模块:在这里可以直接使用函数

from printing_functions import *
unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
completed_models = []
print_models(unprinted_designs, completed_models)
show_completed_models(completed_models)

猜你喜欢

转载自blog.csdn.net/yujing997/article/details/79824048