Python习题七---Python编程:从入门到实践8.1~8.14

消息

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

def display_message():
    print('我学了函数呀')
display_message()

def display_message():
    print('我学了函数呀')
display_message()

喜欢的图书

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

def favourite_book(name):
    print('\nOne of my favourite books is '+name.title()+' in Wonderland')
favourite_book('alice')

D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/first.py

One of my favourite books is Alice in Wonderland

Process finished with exit code 0

T恤

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

#位置实参
def make_shirt(size,model):
    print("The shirt size is "+size+' the modle is '+model)
make_shirt('m','python')

#关键字实参
def make_shirt(size,model):
    print("The shirt size is "+size+' the modle is '+model)
make_shirt(size='m',model='python')

大号T恤

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

def make_shirt(size,model):
    print("The shirt size is "+size+' the modle is '+model)
for i in range(3):
     make_shirt(input("The size:"),'I love Python')
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/first.py
The size:s
The shirt size is s the modle is I love Python
The size:m
The shirt size is m the modle is I love Python
The size:l
The shirt size is l the modle is I love Python

Process finished with exit code 0

城市

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

#我假设了都属于中国哈
def describe_city(city,country='china'):
    active=True
    while active:
        if country !='china':
          print('Please enter a city belonging to China')
          break
        else:
            print(city.title()+' is in '+country.title())
            active=False

describe_city('beijing','china')
describe_city('newyork','usa')
describe_city('shanghai','china')

D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/first.py
Beijing is in China
Please enter a city belonging to China
Shanghai is in China

Process finished with exit code 0

城市名

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

def city_country(city,country):
    full_name=city+','+country
    return full_name.title()
a=city_country('sabtiago','chile')
print('"'+a+'"')
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/first.py
"Sabtiago,Chile"

Process finished with exit code 0

专辑

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

#这里写的时候没省题,没用字典,想看字典写法的可以自行跳到下一题哦,步骤是一样的
def make_album(singer,album,num=''):
    if num:
        full_name=singer+' '+album+' '+num
    else:
        full_name=singer+' '+album
    return full_name.title()
a=make_album('aa','ss','1')
b=make_album('dd','xx')
print(a)
print(b)
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/first.py
Aa Ss 1
Dd Xx

Process finished with exit code 0

用户的专辑

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

def make_album(singer,album,num=''):
    if num:
        albums={'singer':singer,'album':album,'number':num}
    else:
        albums={'singer':singer,'album':album}
    return albums
active=True
while active:
    print('请输入歌手名,专辑名,数量\n')
    singer=input("Singer:")
    album=input("Album:")
    num=input("Number:")
    print(make_album(singer,album,num))
    repeat=input("continue?(yes/no)")
    if repeat=='no':
        active=False
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/first.py
请输入歌手名,专辑名,数量

Singer:aa
Album:aa
Number:1
{'singer': 'aa', 'album': 'aa', 'number': '1'}
continue?(yes/no)no

Process finished with exit code 0

#没用字典的方式
def make_album(singer,album,num=''):
    if num:
        full_name=singer+' '+album+' '+num
    else:
        full_name=singer+' '+album
    return full_name.title()
while True:
    print('请输入歌手名,专辑名,数量\nquit为停止输入')
    s=input("Singer:")
    if s=='quit':
        break
    a=input("Album:")
    if a=='quit':
        break
    n=input('Number:')
    if n=='quit':
        break
    finished=make_album(s,a,n)
    print(finished)
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/first.py
请输入歌手名,专辑名,数量
quit为停止输入
Singer:ss
Album:dd
Number:
Ss Dd
请输入歌手名,专辑名,数量
quit为停止输入
Singer:quit

Process finished with exit code 0

魔术师

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

def show_magicians(names):
    for name in names:
        print('每个魔术师的名字'+name)
magician_name=['aa','ss','dd']
show_magicians(magician_name)
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/first.py
每个魔术师的名字aa
每个魔术师的名字ss
每个魔术师的名字dd

Process finished with exit code 0

了不起的魔术师

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

def show_magicians(names):
    for name in names:
        print('每个魔术师的名字 '+name)
def make_great(names,names2):
    while names:
        name="the great "+names.pop()
        names2.append(name)

magician_name=['aa','ss','dd']
magician_name2=[]

make_great(magician_name,magician_name2)
show_magicians(magician_name2)
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/first.py
每个魔术师的名字 the great dd
每个魔术师的名字 the great ss
每个魔术师的名字 the great aa

Process finished with exit code 0

不变的魔术师

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

def show_magicians(names):
    for name in names:
        print('每个魔术师的名字 '+name)
def make_great(names,names2):
    while names:
        name="the great "+names.pop()
        names2.append(name)

magician_name=['aa','ss','dd']
magician_name2=[]

make_great(magician_name[:],magician_name2)#[:]的形式为增加副本,如果不增加副本,magician_name是没有信息传递的
show_magicians(magician_name2)
show_magicians(magician_name)

D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/first.py
每个魔术师的名字 the great dd
每个魔术师的名字 the great ss
每个魔术师的名字 the great aa
每个魔术师的名字 aa
每个魔术师的名字 ss
每个魔术师的名字 dd

Process finished with exit code 0

三明治

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

def make_sandwich(*toppings):
    print(toppings)
make_sandwich('aa')
make_sandwich('aa','ss')
make_sandwich('cc','vv','ve')
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/first.py
('aa',)
('aa', 'ss')
('cc', 'vv', 've')

Process finished with exit code 0

用户简介

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('xinghe', 'rabbbit',
                                 location='china',
                                 field='dd')
print(user_profile)
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/first.py
{'first_name': 'xinghe', 'last_name': 'rabbbit', 'location': 'china', 'field': 'dd'}

Process finished with exit code 0

汽车

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

def car(manufacturer,type,**info):
    car={}
    car['manufacturer']=manufacturer
    car['type']=type
    for key,value in info.items():
        car[key]=value
    return  car
user=car('咕咕','越野',color='red',toe_package=True)
print(user)
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/first.py
{'manufacturer': '咕咕', 'type': '越野', 'color': 'red', 'toe_package': True}

Process finished with exit code 0

  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

看星河的兔子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值