编写一个名为display_message() 的函数,它打印一个句子,指出你在本章学的是什么。调用这个函数,确认显示的消息正确无误

def display_message():
print("I'm learning about functions in chapter 8\n") display_message()

编写一个名为favorite_book()的函数,其中包含一个名为title的形参。这个函数打印一条消息,如One of my favorite books is Alice in Wonderland。调用这个函数,并将一本图书的名称作为实参传递给它

def favorite_book(title):
print(f"One of my favorite books is {title.title()}.\n") favorite_book("alice in wonderland")

编写一个名为make_shirt() 的函数,它接受一个尺码以及要印到T恤上的字样。这个函数应打印一个句子,概要地说明T恤的尺码和字样,使用位置实参调用这个函数来制作一件T恤;再使用关键字实参来调用这个函数

def make_shirt(size,message):
print(f"The size of T-shirt is {size}.")
print(f"I would liek print {message.title()} in T-shirt.") make_shirt(18,"Python ")
make_shirt(message = 'java 14',size =20)

修改函数make_shirt() ,使其在默认情况下制作一件印有字样“I love Python”的大号T恤。调用这个函数来制作如下T恤:一件印有默认字样的大号T 恤、一件印有默认字样的中号T恤和一件印有其他字样的T恤(尺码无关紧要)

def make_shirt(size = 'large',message='I love Python'):
print(f"The size of T-shirt is {size}.")
print(f"I would liek print {message.title()} in T-shirt.") make_shirt()
make_shirt(message='I love Java',size ='medium')

编写一个名为describe_city()的函数,它接受一座城市的名字以及该城市所属的国家。这个函数应打印一个简单的句子,如Reykjavik is in Iceland 。给用于存储国家的形参指定默认值。为三座不同的城市调用这个函数,且其中至少有一座城市不属于默认国家

def describe_city(city_name='ShangHai',country = 'China'):
print(f"{city_name.title()} is in {country.title()}.") describe_city()
describe_city('Tokyo','Japan')
describe_city('Saint louis','American')

编写一个名为city_country() 的函数,它接受城市的名称及其所属的国家。这个函数应返回一个格式类似于下面这样的字符串:

"Santiago Chile"
def city_country(city_name,country):
city =f'{city_name.title()},{country.title()}'
return city city = city_country('santiago','Chile')
print(city) city = city_country('shanghai','China')
print(city) city = city_country('toyko','japan')
print(city)

编写一个名为make_album() 的函数,它创建一个描述音乐专辑的字典。这个函数应接受歌手的名字和专辑名,并返回一个包含这两项信息的字典。使用这个函数创建三个表示不同专辑的字典,并打印每个返回的值,以核实字典正确地存储了专辑的信息。给函数make_album() 添加一个可选形参,以便能够存储专辑包含的歌曲数。如果调用这个函数时指定了歌曲数,就将这个值添加到表示专辑的字典中。调用这个 函数,并至少在一次调用中指定专辑包含的歌曲数

def make_album(artist_name,album_title,number = None):
album ={'artist_name' : artist_name.title(),'album_title' : album_title.title()}
if number:
album['number'] = number
return album album = make_album('zhou','yehuimei')
print(album) album = make_album('zhou','yehuimei',23)
print(album) album = make_album('kerplunk','punk rock band',1992)
print(album)

在为完成练习8-7编写的程序中,编写一个while 循环,让用户输入一个专辑的歌手和名称。获取这些信息后,使用它们来调用函 数make_album() ,并将创建的字典打印出来。在这个while 循环中,务必要提供退出途径

def make_album(artist_name,album_title,number = None):
album ={'artist_name' : artist_name.title(),'album_title' : album_title.title()}
if number:
album['number'] = number
return album while True:
print("Please enter the album's artist and title")
print("enter 'q' at any time to quit") artist = input('artist name:')
if artist == 'q':
break
title = input('album title:')
if title == 'q':
break
album = make_album(artist,title)
print(album)

创建一个包含多条信息的列表,并将其传递给一个名为show_ message() 的函数,这个函数打印列表中每条信息

def show_message(messages):
for message in messages:
print(message) messages = ['chandler','joey','ross','rachel']
show_message(messages)

拷贝一份8.9的代码,编写一个名为send_message()的函数,打印每条信息后,将信息移动到send_messages列表中,在调用函数后,打印所有的列表确认信息移动正确

def show_message(messages):
for message in messages:
print(message) def send_message(unprinted_messages,completed_messages):
while unprinted_messages:
message =unprinted_messages.pop()
completed_messages.append(message)
show_message(completed_messages) unprinted_messages = ['chandler','joey','ross','rachel']
completed_messages = []
send_message(unprinted_messages,completed_messages)
print(unprinted_messages)
print(completed_messages)

从上一份代码开始,在调用seng_message()的同时,向它传递信息的副本。在调用函数后,打印所有的列表确认信息保留正确

def show_message(messages):
for message in messages:
print(message) def send_message(unprinted_messages,completed_messages):
while unprinted_messages:
message =unprinted_messages.pop()
completed_messages.append(message)
show_message(completed_messages) unprinted_messages = ['chandler','joey','ross','rachel']
completed_messages = []
send_message(unprinted_messages[:],completed_messages)
print(unprinted_messages)
print(completed_messages)

编写一个函数,它接受顾客要在三明治中添加的一系列食材。这个函数只有一个形参(它收集函数调用中提供的所有食材),并打印一条消息,对顾客点的三明治进行概述。调用这个函数三次,每次都提供不同数量的实参

def provide_sandwiches(*toppings):
print("sandwiches adding ..")
for topping in toppings:
print('\t'+topping) provide_sandwiches('roast beef', 'cheddar cheese', 'lettuce', 'honey dijon')
provide_sandwiches('peanut','ice cream','tomato','lemon')
provide_sandwiches('apple','pistachio','cashew nut')

复制前面的程序user_profile.py,在其中调用build_profile() 来创建有关你的简介;调用这个函数时,指定你的名和姓,以及三个描述你的键-值对

def build_profile(first,last,**user_info):
"""Build a dictionary containing everything we know about a use."""
user_info['first_name'] = first
user_info['last_name'] = last
return user_info user_profile = build_profile('albery','einstein',
location = 'princeton',field ='physics')
print(user_profile) user_profile = build_profile('Higos','nut',
location = 'Shanghai',field ='math')
print(user_profile) user_profile = build_profile('Chandler','Bing',
location = 'NewYork',field ='Data Science')
print(user_profile)

编写一个函数,将一辆汽车的信息存储在一个字典中。这个函数总是接受制造商和型号,还接受任意数量的关键字实参。这样调用这个函数:提供必不可 少的信息,以及两个名称—值对,如颜色和选装配件。这个函数必须能够像下面这样进行调用:

car = make_car('subaru','outback',color ='bule',tow_package=True)
def make_car(manufacturer,model,**features):
dic = {
'manufacturer' : manufacturer.title(),
'model' : model.title()
}
for option,feature in features.items():
dic[option] = feature
return dic car1 = make_car('subaru', 'outback', color='blue', tow_package=True)
print(car1)
car2 =make_car('honda','motor',color ='yellow',tow_package = False, year= 1996,speed ='250mph')
print(car2)

将示例print_models.py中的函数放在另一个名为printing_functions. py的文件中;在print_models.py的开头编写一条import 语句,并修改这个文件以使用导 入的函数

#不同目录下需要设置 __init__.py
#本文目录形式为 :
# -books:
# --print_models.py
# -exercises:
# --printing_functions.py
#需要在books目录下新增 __init__.py 然后引用以下代码
import sys
sys.path.append('..')
import books.printing_models

PythonCrashCourse 第八章习题的更多相关文章

  1. C和指针 第八章 习题

    8.5矩阵运算,A是一个x行,y列矩阵,B是y行z列矩阵,把A和B相乘,结果是另外一个x行z列矩阵,每个位置的值由下公式决定,编写函数: #include <stdio.h> void m ...

  2. java编程思想第四版第八章习题

    第一题 package net.mindview.polymorphism; //基类-自行车 class Cycle{ } //子类-单轮车 class Unicycle extends Cycle ...

  3. PythonCrashCourse 第十章习题

    在文本编辑器中新建一个文件,写几句话来总结一下你至此学到的Python知识,其中每一行都以"In Python you can"打头.将这个文件命名为 learning_pytho ...

  4. PythonCrashCourse 第九章习题

    创建一个名为Restaurant 的类,其方法__init__() 设置两个属性: restaurant_name 和cuisine_type 创建一个名为describe _restaurant ( ...

  5. PythonCrashCourse 第二章习题

    2.3 个性化消息:将用户的姓名存到一个变量中,并向该用户显示一条消息.显示的消息应非常简单,如"Hello Eric, would you like to learn some Pytho ...

  6. 20145202马超 《Java程序设计》第五周学习总结

    异常:程序在运行的时候出现不正正常的情况 由来:问题也是可以通过java对不正常情况进行描述后的对象的体现. 问题的划分:(1).严重的问题,java通过error类进行描述,对于error一般不编写 ...

  7. PythonCrashCourse 第三章习题

    PythonCrashCourse 第三章习题 3.1 将一些朋友的姓名存储在一个列表中,并将其命名为names.依次访问该列表中的每个元素,从而将每个朋友的姓名都打印出来 names = ['lih ...

  8. PythonCrashCourse 第四章习题

    Python 从入门到实践第四章习题 4.1想出至少三种你喜欢的比萨,将其名称存储在一个列表中,再使用for 循环将每种比萨的名称都打印出来 修改这个for 循环,使其打印包含比萨名称的句子,而不仅仅 ...

  9. java基础 第八章课后习题

    1.什么是二重循环?在内层循环中使用continue和break语句,程序如何跳转? 答:二重循环就是一个循环结构体内又包含另一个完整的循环结构. continue语句跳转时是跳过了内层循环中的剩余语 ...

随机推荐

  1. Centos 7 静态IP设置

    1.编辑 ifcfg-eth0 文件,vim 最小化安装时没有被安装,需要自行安装不描述. # vim /etc/sysconfig/network-scripts/ifcfg-eth0 2.修改如下 ...

  2. vue学习(十一) v-for使用的注意事项:2.2.0+之后的版本里,当在组件中使用v-for时,key是必须的,它是用来表示唯一身份的

    //html <div id="app"> <div> <label>id <input type="text" v- ...

  3. UWP 自定义密码框控件

    1. 概述 微软官方有提供自己的密码控件,但是控件默认的行为是输入密码,会立即显示掩码,比如 *.如果像查看真实的文本,需要按查看按钮. 而我现在自定义的密码控件是先显示你输入的字符2s,然后再显示成 ...

  4. string类型 C++

    (C++递归预习到了string类型,这个是处理字符串的一个非常好用的东西,在C里面没有.今天来学习一下) 顺便推荐一个很不错的网站:http://c.biancheng.net/view/400.h ...

  5. Day10_ElasticSearch

    学于黑马和传智播客联合做的教学项目 感谢 黑马官网 传智播客官网 微信搜索"艺术行者",关注并回复关键词"乐优商城"获取视频和教程资料! b站在线视频 老师的码 ...

  6. gerrit安装指南

    Gerrit的基本介绍 Gerrit 是一个Git服务器,它基于 git 版本控制系统,使用网页界面来进行审阅工作.Gerrit 旨在提供一个轻量级框架,用于在代码入库之前对每个提交进行审阅,更改将上 ...

  7. PHP stristr() 函数

    实例 查找 "world" 在 "Hello world!" 中的第一次出现,并返回字符串的剩余部分: <?php高佣联盟 www.cgewang.com ...

  8. react引入相同组件时互不影响

    具体代码可以查看我的代码仓库 https://gitee.com/haomYGH/Web20/tree/master/010-React/014-redux-immutable 页面展示 要处理的问题 ...

  9. 学习java 线程池-1: ThreadPoolExecutor

    1. Executor 该接口内只有一个接口方法 :该方法的目的就是执行指定的 Runnable (但会不会执行,或者会不会立马执行,则不一定.因为要取决于整个线程池的状态) Executor 中文的 ...

  10. Angular 10材质的模态弹出示例和教程

    在本教程中,我们将通过示例使用Angular 10材质构建模式弹出窗口. 在这里,我们将研究创建Angular 10项目,安装和设置Angular 10材质,以及创建自定义材质模块文件. 在本教程中, ...