Python编程:从入门到实践(课后习题:第8章 函数)

coding=gbk

coding:utf-8!

8-1消息

def display_message():
“”“显示简单的语句”""
print(“We will learn function from this chapter.”)
display_message()

8-2喜欢的图书

print()
def favorite_book(title): # 注意这里形参不需要加单引号
“”“显示喜欢的图书”""
print("One of my favorite books is " + title + “.”)
favorite_book(‘Alice in Wonderland’) # 注意括号里面的实参要加单引号

8-3 T恤

def make_shirt(size, word):
“”“显示T恤尺码及字样”""
print("\nThe size is " + size + “.”) # 可以在size前面加str()
print("The word on the T-shirt is " + word + “.”)
make_shirt(‘S’, ‘Hello World’)

8-4大号T恤

def make_shirt(size = ‘L’, word = ‘I love Python’):
“”“显示默认‘我爱Python字样’的大号T恤”""
print("\nThe size is " + size + “.”)
print("The word on the T-shirt is " + word + “.”)
make_shirt()
make_shirt(size = ‘M’)
make_shirt(size = ‘S’, word = ‘Hello world’)

8-5城市

def describe_city(city_name, nation = ‘China’):
“”“城市名字及所属国家”""
print("\n" + city_name + " is in " + nation + “.”)
describe_city(‘Beijing’)
describe_city(‘Shenzhen’)
describe_city(‘London’, ‘England’)

8-6城市名

print()
def city_country(city, country):
“”“返回城市及所属国家”""
str = city + ", " + country
return str.title()
number1 = city_country(‘beijing’, ‘china’)
number2 = city_country(‘shenzhen’, ‘china’)
number3 = city_country(‘london’, ‘england’)
print(number1)
print(number2)
print(number3)

8-7专辑

print()
def make_album(singer_name, album_name, number=’’):
“”“返回专辑字典”""
album = {‘singer’: singer_name, ‘album’: album_name}
if number:
album[‘number’] = number
return album
album1 = make_album(‘Jay Chou’, ‘Listen to mother’)
album2 = make_album(‘Singer 2’, ‘Album 2’, 2)
album3 = make_album(‘Singer 3’, ‘Album 3’, 5)
print(album1)
print(album2)
print(album3)

网上他人答案

def make_album(singer, album, num=’’):
albums = {}
if num:
albums[‘singer’] = singer
albums[‘album’] = album
albums[‘num’] = num
else:
albums[‘singer’] = singer
albums[‘album’] = album
return albums
u87 = make_album(‘陈奕迅’, ‘U87’)
u87_num = make_album(‘陈奕迅’, ‘U87’, num=12)
happy = make_album(‘陈奕迅’, ‘我的快乐时代’, num=15)
print(u87)
print(u87_num)
print(happy)

8-8用户的专辑

print()
def make_album(singer_name, album_name, number=’’):
“”“返回专辑字典”""
album = {‘singer’: singer_name, ‘album’: album_name}
if number:
album[‘number’] = number
return album
while True:
print("\nPlease type singer’s name and album’s name:")
print("(enter ‘quit’ any time to quit)")
s_name = input(“Singer name:”)
if s_name == ‘quit’:
break
a_name = input(“Album name:”)
if a_name == ‘quit’:
break
album0 = make_album(s_name, a_name)
print(album0)

8-9魔术师

方法一

print()
unprinted_magicians = [‘earvin johnson’, ‘elymas’, ‘aubrey’, ‘arien’,]
completed_magicians = []
while unprinted_magicians:
current_magician = unprinted_magicians.pop()
print("Printing names: " + current_magician.title())
completed_magicians.append(current_magician)
print(“The following names have been printed:”)
for completed_magician in completed_magicians:
print(completed_magician)

方法二

print()
def print_magicians(unprinted_magicians,completed_magicians):
“”"
模拟打印每个名字,直到没有未打印的名字为止
打印每个名字后,都将其移到列表completed_magicians中
“”"
while unprinted_magicians:
current_magician = unprinted_magicians.pop()
print(“Printing names: " + current_magician.title())
completed_magicians.append(current_magician)
def show_magicians(completed_magicians):
“”“显示打印好的所有名字””"
print(“The following names have been printed:”)
for completed_magician in completed_magicians:
print(completed_magician)
unprinted_magicians = [‘earvin johnson’, ‘elymas’, ‘aubrey’, ‘arien’,]
completed_magicians = []
print_magicians(unprinted_magicians,completed_magicians)
show_magicians(completed_magicians)

网上参考答案

print()
name_list = [‘mag1’,‘mag2’,‘mag3’]
def show_magicians():
for name in name_list:
print(name)
show_magicians()

8-10了不起的魔术师

def print_great(unprinted_magicians, printed_magicians):
while unprinted_magicians:
current_magician = unprinted_magicians.pop()
print("The great " + current_magician.title())
printed_magicians.append(current_magician)
def show_great(printed_magicians):
for printed_magician in printed_magicians:
print(printed_magician)
unprinted_magicians = [‘criss’, ‘jason’, ‘cyril’]
printed_magicians = []
print_great(unprinted_magicians,printed_magicians)
show_great(printed_magicians)

网上参考答案

print()
print(“8-10网上参考答案”)
name_list = [‘mag1’,‘mag2’,‘mag3’]
name_change = []
def make_great(name_list,name_change):
while name_list:
cur = name_list.pop()
cur = 'the great ’ + cur
name_change.append(cur)

def show_magicians(name_change):
for name in name_change:
print(name)

make_great(name_list,name_change)
show_magicians(name_change)

8-11不变的魔术师

网上参考答案

print()
print(“8-11网上参考答案”)
name_list = [‘mag1’,‘mag2’,‘mag3’]
name_change = []

def make_great(name_list,name_change):
while name_list:
cur = name_list.pop()
cur = 'the great ’ + cur
name_change.append(cur)
def show_magicians(name_change):
for name in name_change:
print(name)

make_great(name_list[:],name_change)
show_magicians(name_list)
show_magicians(name_change)

8-12三明治

print("\n8-12三明治")
def make_sandwich(*toppings): # 也可以用ingredients
for topping in toppings:
print(“We will add " + topping + " on sandwich.”)
make_sandwich(‘onions’, ‘green pepper’, ‘oliver’,)
make_sandwich(‘pepper’)
make_sandwich(‘chicken’, ‘oliver’)

8-13用户简介

print("\n8-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(‘tina’, ‘wang’,
location = ‘shenzhen’,
field = ‘finance’,
favorite = ‘reading’,)
print(user_profile)

8-14汽车

print("\n8-14汽车")
def build_car(manufacturer, model, **car_info):
car = {}
car[“car’s manufacturer”] = manufacturer
car[“car’s model”] = model
for key, value in car_info.items():
car[key] = value
return car

car_1 = build_car(‘subaru’, ‘outback’,
color = ‘blue’,
tow_package = True,)
print(car_1)

8-15打印模型

将print_models.py中的函数放在另一个名为printing_functions.py

文件中;在print_models.py的开头编写一条import语句

import printing_functions
unprinted_designs = [‘nokia case’, ‘robot pendant’, ‘dodecahedron’]
completed_models = []
printing_functions.print_models(unprinted_designs, completed_models)
printing_functions.show_completed_models(completed_models)

8-16导入

分别使用语法:

import module_name

print()
import pizza
pizza.make_pizza(15, ‘onion’, ‘green pepper’)

from module_name import function_name

from pizza import make_pizza
make_pizza(12, ‘corn’)

from module_name import function_name as fn

from pizza import make_pizza as mp
mp(10, ‘apple’)

import module_name as mn

import pizza as p
p.make_pizza(22, ‘banana’)

from module_name import *

from printing_functions import *
unprinted_designs = [‘samsung case’, ‘robot pendant’, ‘dodecahedron’]
completed_models = []
printing_functions.print_models(unprinted_designs, completed_models)
printing_functions.show_completed_models(completed_models)

你可能感兴趣的:(Python)