欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

第八章

程序员文章站 2022-07-12 17:59:00
...

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)