admin管理员组

文章数量:819698

Python编程:从入门到实践的动手试一试练习答案(第八章)

第八章 函数 练一练习题参考答案

以下内容非标准答案,是我个人练习内容,仅供参考:

如有不准确,希望指出

#如果希望中文字符可以被Python识别,请在代码内添加如下内容:
# coding=gbk
# ~ #8-1
def display_message():print('函数是带名字的代码块,用于完成具体工作')
display_message()
# ~ #8-2def favorite_book(book):'''book 在这里就是一个形参'''print('学习' + book.title() + ',就是为了好玩!')'''这个 python 是一个实参,调用函数时传递信息给函数'''''' python 就是被储存在函数定义时 book 中'''
favorite_book('python')
# ~ #8-3
def make_shirt(size,types = '英文'):print('T-shirt尺寸: ' + size.upper() +'\n字样形式: ' + types)make_shirt(size = 'l')
# ~ #8-4
def make_shirt(types = 'I love Python',size = 's'):print('T-shirt尺寸: ' + size.upper() +'\n字样内容: ' + types.title())make_shirt()
make_shirt(size = 'l')
make_shirt(size = 'M')
make_shirt(types = 'i miss you')
# ~ #8-5
def describe_city(city,nationality = '中国'):print(city.title() + '地处于' + nationality + '。\n')describe_city('上海')
describe_city('杭州')
describe_city(city = '纽约',nationality = '美国')
def describe_city(city,nationality):city_nationality = city + ',' + nationality + '\n'return city_nationality.title()value = describe_city('上海','中国')
print(value)
value = describe_city('杭州','中国')
print(value)
value = describe_city(city = '纽约',nationality = '美国')
print(value)
# ~ #8-7
def make_album(name,cd_name,cd_num = ''):album = {'歌手':name.title(),'专辑':cd_name.title()}if cd_num:album['歌曲数量'] = cd_numreturn albumvalue = make_album('michael jackson','billie jean')
print(value)
value = make_album('周杰伦','双截棍')
print(value)
value = make_album('张学友','smile','11')
print(value)
# ~ #8-8
def make_album(name,cd_name):album = {'歌手':name.title(),'专辑':cd_name.title()}return albumwhile True:print('\n你对你喜欢的歌手有多了解?')print('我们来做个调查,关闭调查输入‘Q’')name = input('\n输入你喜欢的歌手名字')if name == 'Q':breakcd_name = input('\n输入你知道的专辑名字')if cd_name == 'Q':breakalbum_1 = make_album(name,cd_name)print('\n你喜欢的歌手信息如下,你觉得对吗?\n' + str(album_1))
# ~ #8-9
def show_magician(names):for name in names:message = name + '是著名魔术师!'print(message)
magicians = ['大卫','刘谦','布莱恩']
show_magician(magicians)
# ~ #8-10
def make_great(magicians,fin_magicians):while magicians:pop_magician = magicians.pop()print('正在报名参赛魔术师: ' + pop_magician)fin_magicians.append(pop_magician + ' 加油!')def show_magician(fin_magicians):print('\n已经报名完成名单: ')for name in fin_magicians:print(name)magicians = ['大卫','刘谦','布莱恩']
fin_magicians = []make_great(magicians,fin_magicians)
show_magician(fin_magicians)
# ~ #8-11 调用副本,防止函数修改列表
def make_great(magicians,fin_magicians):while magicians:pop_magician = magicians.pop()print('正在报名参赛魔术师: ' + pop_magician)fin_magicians.append(pop_magician + ' 加油!')def show_magician(fin_magicians):print('\n已经报名完成名单: ')for name in fin_magicians:print(name)magicians = ['大卫','刘谦','布莱恩']
fin_magicians = []
'''
这里直接magicians后加入‘[:]’,就是直接传入副本了
'''
make_great(magicians[:],fin_magicians)
show_magician(fin_magicians)
'''
这里打印就是查看magicians是否被修改
'''
print(magicians)
# ~ #8-12
def make_food(*sandwichs):print('\n你所要的三明治有如下配料:')for sandwich in sandwichs:print('- ' + sandwich)make_food('蔬菜')
make_food('培根','奶酪')
make_food('金枪鱼','酸黄瓜','蛋黄酱')
# ~ #8-13
'''
这个只是把 8.5.2 的内容抄写了一遍而已,没有意义
因为这个练习题目本来就是修改里面的参数值而已,所以就没必要特地新建了
'''
def build_profile(first,last,**user_info):profile={}profile['first_name'] = firstprofile['last_name'] = lastfor key,value in user_info.items():profile[key] = valuereturn profileuser_profile = build_profile('alber','einstein',location='princeton',field = 'physice')print(user_profile)
# ~ #8-14
略
# ~ #8-15 导入模块
'''
建立一个py文件,名字可以随意命名,我这里就命名为> printing_functions.py这个函数,我用的就是上面 8-10 内容,然后分割一下,让自己熟悉一下如何导入模块
'''
def make_great(magicians,fin_magicians):while magicians:pop_magician = magicians.pop()print('正在报名参赛魔术师: ' + pop_magician)fin_magicians.append(pop_magician + ' 加油!')def show_magician(fin_magicians):print('\n已经报名完成名单: ')for name in fin_magicians:print(name)'''
然后这里就是另外一个py文件,我命名为 > print_models.py这个名字也可以随意命名
然后输入一下内容,就可以第一次尝试到导入模块了
'''
from printing_functions import *
magicians = ['大卫','刘谦','布莱恩']
fin_magicians = []make_great(magicians,fin_magicians)
show_magician(fin_magicians)
# ~ #8-16
略
# ~ #8-17
略

本文标签: Python编程从入门到实践的动手试一试练习答案(第八章)