第八章

8-1

def display_message():
    print('I have learnt how to use function in this chapter.') 
    
display_message()

8-2

def favourite_book(title):
	print('One of my favourite book is ' + title + '.')
	
favourite_book('Hunter')

8-3

def make_shirt(size,logo):
	print('The size of the shirt is ' + size + ', and its logo is ' + logo + '.')

make_shirt('L',"Tim")
make_shirt(size = 'L', logo = 'Tim')

8-4

def make_shirt(size = 'L',logo = 'I love python'):
	print('The size of the shirt is ' + size + ', and its logo is ' + logo + '.')

make_shirt()
make_shirt('M')
make_shirt('M',"I love C++")

8-5

def describe_city(city,country = 'China'):
	print(city + ' is in ' + country + '.')
describe_city('Guangzhou')
describe_city('Beijing')
describe_city('Los Angel','America')

8-7

def make_album(name_,singer_):
	album = {'name':name_, 'singer':singer_}
	return album
album_1 = make_album('A',"Jack")
album_2 = make_album('B',"Jay")
album_3 = make_album('C',"Karsa")
print(album_1)
print(album_2)
print(album_3)
def make_album(name_,singer_,num = 0):
	album = {'name':name_, 'singer':singer_}
	if num:
		album['number'] = num
	return album
album_1 = make_album('A',"Jack",10)
album_2 = make_album('B',"Jay",6)
album_3 = make_album('C',"Karsa")
print(album_1)
print(album_2)
print(album_3)

8-8


def make_album(name_,singer_,num = 0):
	album = {'name':name_, 'singer':singer_}
	if num:
		album['number'] = num
	return album
while True:
	name_ = input('Please enter the name of the album:\n')
	singer_ = input('Please enter the name of the singer:\n')
	album = make_album(name_,singer_)
	print(album)
	order = input('Do you want to exit this system?(y/n)\n')
	if order == 'y':
		break

8-9

def show_magicians(magicians):
	for magician in magicians:
		print(magician)
magicians = ['Jack','Larry','David']
show_magicians(magicians)

8-10

def make_magicians(magicians):
	magicians_ = []
	for magician in magicians:
		magician = 'tht Great ' + magician
		magicians_.append(magician)
	return magicians_

def show_magicians(magicians):
	for magician in magicians:
		print(magician)
		
magicians = ['Jack','Larry','David']

magicians = make_magicians(magicians)
show_magicians(magicians)

8-11

def make_magicians(magicians):
	magicians_ = []
	for magician in magicians:
		magician = 'tht Great ' + magician
		magicians_.append(magician)
	return magicians_

def show_magicians(magicians):
	for magician in magicians:
		print(magician)
		
magicians = ['Jack','Larry','David']

magicians_ = make_magicians(magicians)
show_magicians(magicians)
show_magicians(magicians_)

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
	
user_profile = build_profile('Zilin', 'Huang', location = 'Sun Yat-sen',
job = 'student', field = 'computer sicence')
print(user_profile)

8-16

from user_profile import build_profile
pro = build_profile('Zilin', 'Huang', location = 'Sun Yat-sen',
job = 'student', field = 'computer sicence')
print(pro)

from user_profile import build_profile as fn
pro = fn('Zilin', 'Huang', location = 'Sun Yat-sen',
job = 'student', field = 'computer sicence')
print(pro)

import user_profile as mn
pro = mn.build_profile('Zilin', 'Huang', location = 'Sun Yat-sen',
job = 'student', field = 'computer sicence')
print(pro)

from user_profile import *
pro = build_profile('Zilin', 'Huang', location = 'Sun Yat-sen',
job = 'student', field = 'computer sicence')
print(pro)

import user_profile
pro = user_profile.build_profile('Zilin', 'Huang', location = 'Sun Yat-sen',
job = 'student', field = 'computer sicence')
print(pro)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值