Python入门-函数

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

def display_message():
    """简单地显示本章所学"""
    print("八章将要学习编写函数。包括向函数传递信息,\
让函数处理信息并返回一个或一组值,\
还将学习如何将函数存储在被称为模块的独立文件中,\
让主程序文件的组织更为有序")

display_message()


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

def favorite_book(title):
    """打印喜欢的书目"""
    print("One of my favorite books is " + title.title() + ".")
        
favorite_book("C++ Premier")


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

def make_shirt(size, sentence):
    """输出尺码以及字样"""
    print('This shirt is ' + size + ', and "' + sentence + '" will be wirte on it.')

make_shirt("large", "I love Python")
make_shirt(sentence = "I love C++ too", size = "small")


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

def make_shirt(size, sentence = "I love Python"):
    """输出尺码以及字样"""
    print('This shirt is ' + size + ', and "' + sentence + '" will be wirte on it.')

make_shirt("large")
make_shirt("medium")
make_shirt(sentence = "I love C++ too", size = "small")


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

def describe_city(city, country = "China"):
    """简单打印一个城市属于哪个国家"""
    print(city.title() + " is in " + country.title() + ".")

describe_city("GuangZhou")
describe_city("Beijing")
describe_city("New York", "America")


8-7 专辑 :编写一个名为make_album() 的函数,它创建一个描述音乐专辑的字典。这个函数应接受歌手的名字和专辑名,并返回一个包含这两项信息的字典。使用这个函数创建三个表示不同专辑的字典,并打印每个返回的值,以核实字典正确地存储了专辑的信息。

def make_album(name, album):
    dic = {}
    dic[name] = album
    return dic

dic1 = make_album("Maroon 5", "《Hands All Over》")
dic2 = make_album("Coldlay", "《X&Y》")
dic3 = make_album("Taylor Swift", "《Red》")

for name, album in dic1.items():
    print(name + ", " + album)

for name, album in dic2.items():
    print(name + ", " + album)

for name, album in dic3.items():
    print(name + ", " + album)

8-9 魔术师 :创建一个包含魔术师名字的列表,并将其传递给一个名为show_magicians() 的函数,这个函数打印列表中每个魔术师的名字。

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

lis = ['Henry', 'James', 'Cury']
show_magicians(lis)


8-10 了不起的魔术师 :在你为完成练习8-9而编写的程序中,编写一个名为make_great() 的函数,对魔术师列表进行修改,在每个魔术师的名字中都加入字样“the Great”。调用函数show_magicians() ,确认魔术师列表确实变了。

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

def make_great(lis):
    for i in range(0, len(lis)):
        lis[i] = "the Great " + lis[i]

lis = ['Henry', 'James', 'Cury']
make_great(lis)
show_magicians(lis)


8-11 不变的魔术师 :修改你为完成练习8-10而编写的程序,在调用函数make_great() 时,向它传递魔术师列表的副本。由于不想修改原始列表,请返回修改后的列表,并将其存储到另一个列表中。分别使用这两个列表来调用show_magicians() ,确认一个列表包含的是原来的魔术师名字,而另一个列表包含的是添加了字样“the Great”的魔术师名字。

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

def make_great(lis):
    for i in range(0, len(lis)):
        lis[i] = "the Great " + lis[i]
    return lis

lis1 = ['Henry', 'James', 'Cury']
lis2 = make_great(lis1[:])
show_magicians(lis1)
show_magicians(lis2)


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

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('Liu', 'Henry', location='GuangZhou',
                             field='computer science', favorite='run')

print(user_profile)


8-16 导入 :选择一个你编写的且只包含一个函数的程序,并将这个函数放在另一个文件中。在主程序文件中,使用下述各种方法导入这个函数,再调用它:

import module_name
from module_name import function_name
from module_name import function_name as fn
import module_name as mn
from module_name import *

module.py:

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

import module_name:

import module
user_profile = module.build_profile('Liu', 'Henry', location='GuangZhou',
                             field='computer science', favorite='run')

print(user_profile)


from module_name import function_name:

from module import build_profile
user_profile = build_profile('Liu', 'Henry', location='GuangZhou',
                             field='computer science', favorite='run')

print(user_profile)

from module_name import function_name as fn:

from module import build_profile as fn
user_profile = fn('Liu', 'Henry', location='GuangZhou',
                             field='computer science', favorite='run')

print(user_profile)

import module_name as mn:

import module as mn
user_profile = mn.build_profile('Liu', 'Henry', location='GuangZhou',
                             field='computer science', favorite='run')

print(user_profile)

from module_name import *:
from module import *
user_profile = build_profile('Liu', 'Henry', location='GuangZhou',
                             field='computer science', favorite='run')

print(user_profile)


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值