【Hello Python World】Week 4(2):函数
2018-04-01 12:48
387 查看
8-1 消息
8-2 喜欢的图书
8-3 T恤
8-4 大号T恤
8-5 城市
8-6 城市名
8-7 专辑
8-8 用户的专辑
8-9 魔术师
8-10 了不起的魔术师
8-11 不变的魔术师
8-12 三明治
8-13 用户简介
8-14 汽车
8-15 打印模型
第八章主要是继续介绍Python中的函数,内容相对之前的章节复杂一点点
cc89
称。获取这些信息后,使用它们来调用函 数
它的输出是 JP JJ PP,居然没有任何变化,我惊了
注意:
如果要改变列表内容,需要用下标访问的方式
输出:
注意:调用文件和被调文件需要在相同目录下
8-2 喜欢的图书
8-3 T恤
8-4 大号T恤
8-5 城市
8-6 城市名
8-7 专辑
8-8 用户的专辑
8-9 魔术师
8-10 了不起的魔术师
8-11 不变的魔术师
8-12 三明治
8-13 用户简介
8-14 汽车
8-15 打印模型
第八章主要是继续介绍Python中的函数,内容相对之前的章节复杂一点点
8-1 消息
编写一个名为display_message()的函数,它打印一个句子,指出你在本章学的是什么。调用这个函数,确认显示的消息正确无误。
def display_message(): print("Let's learn function in Python!") display_message() #输出 Let's learn function in Python!
8-2 喜欢的图书
编写一个名为favorite_book()的函数,其中包含一个名为
title的形参。这个函数打印一条消息,如
One of my favorite books is Alice in Wonderland。调用这个函数,并将一本图书的名称作为实参传递给它。
def favorite_book(title): print("The book I borrowed is " + title + ".") favorite_book("Alice in Wonderland") #输出 The book I borrowed is Alice in Wonderland.
8-3 T恤
编写一个名为make_shirt()的函数,它接受一个尺码以及要印到T恤上的字样。这个函数应打印一个句子,概要地说明T恤的尺码和字样。 使用位置实参调用这个函数来制作一件T恤;再使用关键字实参来调用这个函数。
def make_shirt(size, type): print("The shirt I made is " + type + " and the size is " + size + ".") make_shirt("M", "Rag") make_shirt(type = "School-style", size = "L") #输出 #The shirt I made is Rag and the size is M. #The shirt I made is School-style and the size is L.
8-4 大号T恤
修改函数make_shirt(),使其在默认情况下制作一件印有字样“I love Python”的大号T恤。调用这个函数来制作如下T恤:一件印有默认字样的大号T 恤、一件印有默认字样的中号T恤和一件印有其他字样的T恤(尺码无关紧要)。
def make_shirt(size = "M", slogan = "I love C++"): print("The slogan shirt I made is " + slogan + " and the size is " + size + ".") make_shirt("L", "I love Python") make_shirt("L") make_shirt() #输出 #The slogan shirt I made is I love Python and the size is L. #The slogan shirt I made is I love C++ and the size is L. #The slogan shirt I made is I love C++ and the size is M.
8-5 城市
编写一个名为describe_city()的函数,它接受一座城市的名字以及该城市所属的国家。这个函数应打印一个简单的句子,如
Reykjavik is in Iceland。给用于存储国家的形参指定默认值。为三座不同的城市调用这个函数,且其中至少有一座城市不属于默认国家。
def describe_city(city = "Guangzhou"): if city == "Guangzhou": print(city + " is in China") elif city == "New York": print(city + " is in America") else: print(city + " is in Japan") describe_city() describe_city("New York") describe_city("Tokyo") #输出 #Guangzhou is in China #New York is in America #Tokyo is in Japan
8-6 城市名
编写一个名为city_country()的函数,它接受城市的名称及其所属的国家。这个函数应返回一个格式类似于下面这样的字符串:
"Santiago, Chile"
def city_country(city = "Guangzhou"): if city == "Guangzhou": print(city + ", China") elif city == "New York": print(city + ", America") else: print(city + ", Japan") city_country("Guangzhou") #输出 Guangzhou, China
8-7 专辑
编写一个名为make_album()的函数,它创建一个描述音乐专辑的字典。这个函数应接受歌手的名字和专辑名,并返回一个包含这两项信息的字典。使用这个函数创建三个表示不同专辑的字典,并打印每个返回的值,以核实字典正确地存储了专辑的信息。 给函数
make_album()添加一个可选形参,以便能够存储专辑包含的歌曲数。如果调用这个函数时指定了歌曲数,就将这个值添加到表示专辑的字典中。调用这个函数,并至少在一次调用中指定专辑包含的歌曲数。
def make_album(singer, aname, num = -1): album = {} album["singer"] = singer album["album_name"] = aname if num != -1: album["song_num"] = num return album print(make_album("Taylor Swift", "Red")) print(make_album("Taylor Swift", "Red", 14)) #输出 #{'singer': 'Taylor Swift', 'album_name': 'Red'} #{'singer': 'Taylor Swift', 'album_name': 'Red', 'song_num': 14}
8-8 用户的专辑
在为完成练习8-7编写的程序中,编写一个
while循环,让用户输入一个专辑的歌手和名
cc89
称。获取这些信息后,使用它们来调用函 数
make_album(),并将创建的字典打印出来。在这个
while循环中,务必要提供退出途径。
def make_album(singer, aname, num = -1): album = {} album["singer"] = singer album["album_name"] = aname if num != -1: album["song_num"] = num return album while True: string1 = input("Enter the artist, or press 'Q' to quit: ") if string1 == 'Q': break; string2 = input("Enter the album name: ") num = int(input("Enter the song number, or 0 to skip: ")) if num == 0: print(make_album(string1, string2)) else: print(make_album(string1, string2, num)) #Enter the artist, or press 'Q' to quit: Taylor Swift #Enter the album name: Red #Enter the song number, or 0 to skip: 0 #{'singer': 'Taylor Swift', 'album_name': 'Red'} #Enter the artist, or press 'Q' to quit: Taylor Swift #Enter the album name: Red #Enter the song number, or 0 to skip: 14 #{'singer': 'Taylor Swift', 'album_name': 'Red', 'song_num': 14} #Enter the artist, or press 'Q' to quit: Q #Press any key to continue . . .
8-9 魔术师
创建一个包含魔术师名字的列表,并将其传递给一个名为show_magicians()的函数,这个函数打印列表中每个魔术师的名字。
def show_magicians(magicians): for magician in magicians: print(magician, end = " ") magician = ['JP', 'JJ', 'PP'] show_magicians(magician) #输出 #JP JJ PP
8-10 了不起的魔术师
在你为完成练习8-9而编写的程序中,编写一个名为
make_great()的函数,对魔术师列表进行修改,在每个魔术师的名字中都加入字样
“the Great”。调用函数
show_magicians(),确认魔术师列表确实变了。
def make_great(magicians): for magician in magicians: magician = "the great " + magician magicians = ['JP', 'JJ', 'PP'] make_great(magicians) show_magicians(magicians)
它的输出是 JP JJ PP,居然没有任何变化,我惊了
def show_magicians(magicians): for magician in magicians: print(magician.title()) def make_great(magicians): for i in range(0,len(magicians)): magicians[i] = "the great " + magicians[i] magicians = ['JP', 'JJ', 'PP'] make_great(magicians) show_magicians(magicians) #输出 #The Great Jp #The Great Jj #The Great Pp
注意:
如果要改变列表内容,需要用下标访问的方式
title()方法是对句子的每一个单词都首字母大写的
8-11 不变的魔术师
修改你为完成练习8-10而编写的程序,在调用函数
make_great()时,向它传递魔术师列表的副本。由于不想修改原始列表,请返回修改后的 列表,并将其存储到另一个列表中。分别使用这两个列表来调用
show_magicians(),确认一个列表包含的是原来的魔术师名字,而另一个列表包含的是添加了字 样
“the Great”的魔术师名字。
def show_magicians(magicians): print("After change: ", end = "") print(magicians) def make_great(magicians): for i in range(0,len(magicians)): magicians[i] = "the great " + magicians[i] print("Change in function 'make_great()':", end = " ") print(magicians) magicians = ['JP', 'JJ', 'PP'] make_great(magicians[:]) show_magicians(magicians) #输出 #Change in function 'make_great()': ['the great JP', 'the great JJ', 'the great PP'] #After change: ['JP', 'JJ', 'PP']
8-12 三明治
编写一个函数,它接受顾客要在三明治中添加的一系列食材。这个函数只有一个形参(它收集函数调用中提供的所有食材),并打印一条消息,对顾客 点的三明治进行概述。调用这个函数三次,每次都提供不同数量的实参。def foods(*fs): print("I will add ",end = "") for f in fs: print(f, end = ' ') print(".") foods("egg") foods("egg", "bread") foods("egg", "bread", "vegetables") #输出 #I will add egg . #I will add egg bread . #I will add egg bread vegetables .
8-13 用户简介
复制前面的程序user_profile.py,在其中调用
build_profile()来创建有关你的简介;调用这个函数时,指定你的名和姓,以及三个描述你的键-值对。
def user_profile(**user): for key, value in user.items(): print(key, end = ' = ') print(value, end = ' ') print("") user_profile(first_name="JP") user_profile(first_name="JP", last_name="Wang") user_profile(first_name="JP", last_name="Wang", age=19) #输出 #first_name = JP #first_name = JP last_name = Wang #first_name = JP last_name = Wang age = 19
8-14 汽车
编写一个函数,将一辆汽车的信息存储在一个字典中。这个函数总是接受制造商和型号,还接受任意数量的关键字实参。这样调用这个函数:提供必不可 少的信息,以及两个名称—值对,如颜色和选装配件。这个函数必须能够像下面这样进行调用:car = make_car('subaru', 'outback', color='blue', tow_package=True)。
def make_car(inc, type, **features): li = [inc, type, features] return li print(make_car('subaru', 'outback', color='blue', tow_package=True)) #输出 #['subaru', 'outback', {'color': 'blue', 'tow_package': True}]
8-15 打印模型
将示例print_models.py中的函数放在另一个名为
printing_functions.py的文件中;在
print_models.py的开头编写一条
import语句,并修改这个文件以使用导入的函数。
#print_models.py from printing_functions import prtfn prtfn()
#printing_functions.py def prtfn(): print("Hello Python World!")
输出:
Hello Python World!
注意:调用文件和被调文件需要在相同目录下
相关文章推荐
- 【Hello Python World】Class Notes of Week 2
- 【Hello Python World】Week 3(1):if语句
- 【Hello Python World】Week 1(2):变量和简单数据类型
- 【Hello Python World】Class Notes of Week 4
- 【Hello Python World】Class Notes of Week 5
- 【Hello Python World】Week 4(1):用户输入和while循环
- 【Hello Python World】Week 5(2):文件和异常
- 【Hello Python World】Week 5(1):类
- 【Hello Python World】Week 1(1):探索 & 展望
- cocos2d-x: 死磕"HelloWorld"(1)——入口函数及其带来的疑问
- Hello,World! Python.
- Ubuntu:python helloworld.py SyntaxError: invalid syntax
- 20080828 python HelloWorld
- 【Hacker Rank】01.Say "Hello, World!" With Python
- JNI学习--通过Java应用调用C语言的函数打印hello,world
- 解剖Nginx·模块开发篇(3)ngx_http_hello_world_module 模块的基本函数实现
- 第一周作业_hello python world
- (转) Eclipse - Python - Installation of PyDev with a Python Hello World tutorial
- (List)写一个函数reverseList,该函数能够接受一个List,然后把该List 倒序排列。 例如: List list = new ArrayList(); list.add(“Hello”); list.add(“World”); list.add(“Learn”); //此时list 为Hello World Learn rever
- 第一个hello world in python