Python第八章课后作业(函数)

8-1 消息:

def display_message():
	print("I learn function in this chapter")

display_message()

运行结果:


8-2 喜欢的图书:

def favorite_book(bookname):
	print("One of my favorite books is "+bookname.title())

favorite_book("Alice in Wonderland")

运行结果:


8-3 T恤:

def make_shirt(size,word):
	print("size:",size,"\nword:",word)
make_shirt(100,'good')
make_shirt(size=100,word='good')

运行结果:


8-4 大号T恤:

def make_shirt(size,word='I love Phthon'):
	print("size:",size,"\nword:",word)
make_shirt('L')
make_shirt('M')
make_shirt('S',word='good')

运行结果:


8-5 城市:

def describe_city(city,country='China'):
	print(city,'is in',country)
describe_city("Guangzhou")
describe_city("Shenzhen")
describe_city('NewYork','America')

运行结果:


8-6 城市名:

def city_country(city,country):
	return city.title()+","+country.title()#也可以返回两个值,类型是元组
print(city_country("Guangzhou",'China'))
print(city_country('Shenzhen','China'))
print(city_country('newYork','america'))

运行结果:


8-7 专辑:

def make_album(singer,album,num=0):
	res={"singer":singer,"album":album}
	if num!=0:
		res['num']=num
	return res
print(make_album('Jaychou','qinghuaci'))
print(make_album('zhangjie','taiyang'))
print(make_album('lironghao','libai',3))

运行结果:


8-8 用户的专辑:

def make_album(singer,album,num=0):
	res={"singer":singer,"album":album}
	if num!=0:
		res['num']=num
	return res

while True:
	singer=input('Please input the singer name,if your want to quit,input quit:\n')
	if singer=='quit':
		break
	album=input('Please input the album name,if your want to quit,input quit:\n')
	if album=='quit':
		break
	num=input('Please input the number of songs,if your want to quit,input quit:\n')
	if num=='quit':
		break
	print(make_album(singer,album,int(num)))
运行结果:


8-9 魔术师:

def show_magicians(arr):
	print(arr)
arr=['ksb','lmd','hyk']
show_magicians(arr)

运行结果:


8-10 了不起的魔术师:

def show_magicians(arr):
	print(arr)
arr=['ksb','lmd','hyk']
show_magicians(arr)

def make_great(arr):
	for x in range(len(arr)):
		arr[x]='the Great '+arr[x]

make_great(arr)
show_magicians(arr)

运行结果:


8-11 不变的魔术师:

def show_magicians(arr):
	print(arr)

def make_great(arr):
	for i in range(len(arr)):
		arr[i]='the Great '+arr[i]
	return arr

arr=['ksb','lmd','hyk']
show_magicians(arr)
show_magicians(make_great(arr[:]))
show_magicians(arr)

运行结果:


8-12 三明治:

def sandwich(*sands):#任意元素数量的元组
	print(sands)
sandwich('apple','banana','orange')
sandwich('apple','orange')
sandwich('orange')
运行结果:

8-13 用户简介:

def build_profile(first,last,**usr_info):#任意数量键值对的字典
	profile={}
	profile["fist name"]=first
	profile["last name"]=last
	for key,value in usr_info.items():
		profile[key]=value
	return profile
#传递键值对时用下面的形式,键会自动转换为字符串有点意外
user_profile=build_profile('Ke','Sibo',sex='male',height='170cm')
for key,value in user_profile.items():
	print(key+":"+value)
运行结果:

8-14 汽车:

def make_car(mader,number,**usr_info):#任意数量键值对的字典
	profile={}
	profile["mader".title()]=mader
	profile["number".title()]=number
	for key,value in usr_info.items():
		profile[key]=value
	return profile
#传递键值对时用下面的形式,键会自动转换为字符串有点意外
user_profile=make_car('China','6',color='red',decoration='windows')
for key,value in user_profile.items():
	print(key+":"+value)

运行结果:


8-15略

8-16 导入:

分别去掉注释符号即可

在模块myfunc中:

def myprint():
	print('hhh')

在其他模块中调用:

# import myfunc
# myfunc.myprint()

# from myfunc import myprint
# myprint()

# from myfunc import myprint as mp
# mp()

# import myfunc as mf
# mf.myprint()

# from myfunc import *
# myprint()

运行结果:


8-17 略


猜你喜欢

转载自blog.csdn.net/qq_36325159/article/details/79732386