Python作业(8.1-8.16)

Python第8章作业

8-1 消息 : 编写一个名为display_message() 的函数, 它打印一个句子, 指出你在本章学的是什么。 调用这个函数, 确认显示的消息正确无误。

def display_message():
	print("In this chapter,I will learn how to use function")
display_message() 



8-2 喜欢的图书 : 编写一个名为favorite_book() 的函数, 其中包含一个名为title 的形参。 这个函数打印一条消息, One of my favorite books is Alice in Wonderland 。 调用这个函数, 并将一本图书的名称作为实参传递给它。

def favorite_book(title):
	print("ne of my favorite books is "+title)
favorite_book("Pride and prejudice")



8-3 T恤 : 编写一个名为make_shirt() 的函数, 它接受一个尺码以及要印到T恤上的字样。 这个函数应打印一个句子, 概要地说明T恤的尺码和字样。
使用位置实参调用这个函数来制作一件
T恤; 再使用关键字实参来调用这个函数。

def make_shirt(size,pattern):
	print("The size of this shirt is :"+size)
	print("The pattern of this shirt is :"+pattern)
make_shirt("M","cat")
make_shirt(size="L",pattern="dog")


8-4 大号T恤 : 修改函数make_shirt() , 使其在默认情况下制作一件印有字样“I love Python”的大号T恤。 调用这个函数来制作如下T恤: 一件印有默认字样的大号T恤、 一件印有默认字样的中号T恤和一件印有其他字样的T恤(尺码无关紧要) 。

def make_shirt(size='XL',pattern='I love Python'):
	print("The size of this shirt is :"+size)
	print("The pattern of this shirt is :"+pattern)
make_shirt()
make_shirt(size="M")
make_shirt('XL',pattern='I love C')



8-6 城市名 : 编写一个名为city_country() 的函数, 它接受城市的名称及其所属的国家。 这个函数应返回一个格式类似于下面这样的字符串:

def city_country(city,country):
	string='"'+city+","+country+'"'
	return string
s=city_country("Guangzhou","China")
print(s)
	


8-7 专辑 : 编写一个名为make_album() 的函数, 它创建一个描述音乐专辑的字典。 这个函数应接受歌手的名字和专辑名, 并返回一个包含这两项信息的字典。 使用这个函数创建三个表示不同专辑的字典, 并打印每个返回的值, 以核实字典正确地存储了专辑的信息。给函数make_album() 添加一个可选形参, 以便能够存储专辑包含的歌曲数。 如果调用这个函数时指定了歌曲数, 就将这个值添加到表示专辑的字典中。 调用这个函数, 并至少在一次调用中指定专辑包含的歌曲数

def make_album(sing,albu,siz=''):
	 if siz:
		 r={'singer':sing,'album':albu,'size':siz}
		 return r
	 else:
	    r={'singer':sing,'album':albu}
	    return r
new1=make_album('Zhou Jielun','QiLixiang','10')
new2=make_album('Chen Li','Ru ye')
new3=make_album('Xu Wei','Wen nuan')
print(new1)
print(new2)
print(new3)


8-9 魔术师 : 创建一个包含魔术师名字的列表, 并将其传递给一个名为 show_magicians() 的函数, 这个函数打印列表中每个魔术师的名字。
def show_magicians(names):
	for name in names:
		print(name)
name=['Jane','Jason','Helen']
show_magicians(name)


8-13 用户简介 : 复制前面的程序user_profile.py, 在其中调用build_profile() 来创建有关你的简介; 调用这个函数时, 指定你的名和姓, 以及三个描述你的键-
对。

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('Yuan', 'Xu',location='Guangzhou',field='Panyu',school='Sun Yat-san')
print(user_profile)


8-15 打印模型 : 将示例print_models.py中的函数放在另一个名为printing_functions.py的文件中; 在print_models.py的开头编写一条import 语句, 并修改这个文件以使用导入的函数。

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)
	unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
	completed_models = []
	print_models(unprinted_designs, completed_models)
import_use.py:
from  printing_functions import print_models
unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
completed_models = []
print_models(unprinted_designs, completed_models)



猜你喜欢

转载自blog.csdn.net/qq_36755175/article/details/79732321
8.1