《Python编程 从入门到实践》第八章习题选做

8-1 消息

def display_message():
    print("I am learning about the function of Python.")

display_message()

输出:

I am learning about the function of Python.

8-2 喜欢的图书

def favorite_book(title):
    msg = "One of my favorite books is "
    print(msg + title.title() + ".")

favorite_book("robinson crusoe")

输出:

One of my favorite books is Robinson Crusoe.

8-3 T恤

def make_shirt(size, word):
    print("This " + size + ' shirt is printed with "' + word + '".')

make_shirt("big", "I love Python")
make_shirt(word="I love Python", size="big")

输出:

This big shirt is printed with "I love Python".
This big shirt is printed with "I love Python".

8-4 大号T恤

def make_shirt(size="big", word="I love Python"):
    print("This " + size + ' shirt is printed with "' + word + '".')

make_shirt()
make_shirt("medium")
make_shirt("small", "Hello World")

输出:

This big shirt is printed with "I love Python".
This medium shirt is printed with "I love Python".
This small shirt is printed with "Hello World".

8-5 城市

def describe_city(name, country="China"):
    print(name + " is in " + country)

describe_city("Bei Jing")
describe_city("New York", "America")
describe_city(country="England", name="London")

输出:

Bei Jing is in China
New York is in America
London is in England

8-6 城市名

def city_country(name, country):
    msg = name.title() + ", " + country.title()
    return msg

print(city_country("Bei Jing", "China"))
print(city_country("New York", "America"))
print(city_country("London", "England"))

输出:

Bei Jing, China
New York, America
London, England

8-7 专辑

def make_album(singer, album_name, size=''):
    album = {
        'singer_name': singer,
        'album_name': album_name
    }
    if size:
        album['size'] = size
    return album

print(make_album('Jay Chou', 'Waiting For You'))
print(make_album('Eason Chan', 'Sigh & Miss', 3))
print(make_album('Luhan', 'Imagination'))

输出:

{'singer_name': 'Jay Chou', 'album_name': 'Waiting For You'}
{'singer_name': 'Eason Chan', 'album_name': 'Sigh & Miss', 'size': 3}
{'singer_name': 'Luhan', 'album_name': 'Imagination'}

8-8 用户的专辑

def make_album(singer, album_name, size=''):
    album = {'singer_name': singer, 'album_name': album_name}
    if size:
        album['size'] = size
    return album

while True:
    print("Please inpuy singer's name and his album: ")
    print("(enter 'q' at any time to quit)")
    singer = input("Singer name: ")
    if singer == 'q':
        break

    album = input("Album name; ")
    if album == 'q':
        break

    print(make_album(singer, album))

输出:

Please inpuy singer's name and his album:
(enter 'q' at any time to quit)
Singer name: Jay Chou
Album name; Waiting For You
{'singer_name': 'Jay Chou', 'album_name': 'Waiting For You'}
Please inpuy singer's name and his album:
(enter 'q' at any time to quit)
Singer name: Eason Chan
Album name; Sigh & Miss
{'singer_name': 'Eason Chan', 'album_name': 'Sigh & Miss'}
Please inpuy singer's name and his album:
(enter 'q' at any time to quit)
Singer name: Luhan
Album name; Imagination
{'singer_name': 'Luhan', 'album_name': 'Imagination'}
Please inpuy singer's name and his album:
(enter 'q' at any time to quit)
Singer name: q

8-11 不变的魔术师

def show_magicians(magicians):
    for magician in magicians:
        print(magician)


def make_great(magicians):
    ret = []
    while magicians:
        mag = magicians.pop()
        mag = "the Great " + mag
        ret.append(mag)
    return ret


magicians = ['Bob', 'John', 'Amy', 'Tony', 'Sarah']
magicians1 = make_great(magicians[:])
show_magicians(magicians)
print()
show_magicians(magicians1)

输出:

Bob
John
Amy
Tony
Sarah
the Great Sarah
the Great Tony
the Great Amy
the Great John
the Great Bob

8-12 三明治

def describe_Sandwich(*toppings):
    print("Needed raw materials: ", toppings)

describe_Sandwich('onion')
describe_Sandwich('onion', 'flour')
describe_Sandwich('Ham', 'onion', 'Bacon')

输出:

Needed raw materials:  ('onion',)
Needed raw materials:  ('onion', 'flour')
Needed raw materials:  ('Ham', 'onion', 'Bacon')

8-14 汽车

def make_car(Manufacturer, Model, **msg):
    message = {}
    message['Manufacturer'] = Manufacturer,
    message['Model'] = Model
    for key, value in msg.items():
        message[key] = value
    return message


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

输出:

{'Manufacturer': ('subaru',), 'Model': 'outback', 'color': 'blue', 'tow_package': True}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值