Python编程:从入门到实践——练习题答案(第八章)

8-3制作一件T恤

def make_shirt(size,pattern):
    print("The T-shirt size is "+size+" and pattern is "+pattern+". ")
make_shirt(pattern='c',size='1')

8-4设置默认样式的T恤

def make_shirt(size,pattern='I love you'):
    print("The T-shirt size is '"+size+"' and pattern is '"+pattern+"'. ")
make_shirt(size='3')
make_shirt(size='2')
make_shirt(size='1',pattern='sf')

8-6城市名输出

def city_country(city,country):
    a=city+','+country

    return print('"'+a+'"')
city_country('shanghai','china')

8-7专辑

def make_album(name,zhuanji,number=''):
    singer={'name':name,'zhuanji':zhuanji}
    if number:
        singer['number']=number
    return print(singer)
make_album('zhoujielun','yiranfantexi',number=3000000)
make_album('maobuyi','juaniaoguilin')

8-8用户的专辑

def make_album(name,zhuanji,number=''):
    singer={'name':name,'zhuanji':zhuanji}
    if number:
        singer['number']=number
    return print(singer)

while True:
    print("\nif you wang to finish it, please input 'q'. ")
    a=input("please input a name :")
    if a=='q':
        break
    b=input("please input a zhuanji :")
    if b=='q':
        break
make_album(a, b)

8-9打印魔术师名字

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

magic_names=['a','b','c']
show_magicians(magic_names)

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

#显示名字的函数
def show_magicians(names):
    for name in names:
        print(name)
#修改列表的函数
def make_great(names):
    com_names=[]#修改后放入此列表
    while names:
        current_name=names.pop()#中间传递量
        com_names.append("The Great "+current_name)
    for name in com_names:#传递回原列表
        names.append(name)
#主程序
magic_names=['a','b','c']
make_great(magic_names)
show_magicians(magic_names)

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

def show_magicians(names):
    for name in names:
        print(name)
def make_great(names):
    com_names=[]
    while names:
        current_name=names.pop()
        com_names.append("The Great "+current_name)
    return com_names

magic_names=['a','b','c']
copy_names=make_great(magic_names[:])
print("原列表:")
show_magicians(magic_names)
print("新列表:")
show_magicians(copy_names)

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

def sandwich_make(*toppings):
print("\nMaking a sandwich with the following toppings:")
    for topping in toppings:
        print("- " + topping)               
sandwich_make('a','s','d','f')
sandwich_make('1','2')
sandwich_make('j')

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('张','三',Age='11',Add='1',Tel='5675')
print(user_profile)

8-14 汽车
编写一个函数,将一辆汽车的信息存储在一个字典中。
这个函数总是接受制造商和型号,还接受任意数量的关键字实参。
这样调用这个函数:提供必不可少的信息,以及两个名称-值对,如颜色和选装配件。
这个函数必须能够像下面这样进行调用:
car = make_car(“subaru”,“outback”,color=“blue”,two_package=True)
打印返回的字典,确认正确地处理了所有的信息。

def make_car(manufacturer,model,**car_info):
    profile ={}
    profile['manufacturer'] = manufacturer
    profile['model'] = model
    for key,value in car_info.items():
        profile[key] = value
    return profile

car = make_car('subaru','outback',color='blue',two_package=True)
print(car)

函数导入:
(1)从库里导入from XXX import XXX
(2)导入并改名import XXX as XX
(3)导入全部 from XXX import *

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值