《Python编程从入门到实践》练习题代码实现第八章

#8-1 消息
def display_message():
    print("你学了什么?")
display_message()

#8-2 喜欢的图书
def favorite_book(book):
    print("one of my favorite books is %s"%book)
favorite_book('ni')

#8-3 T恤
#位置调参
def make_shirt(size,words):
    print(f"\nthe t-shirt size is {size},the words is {words}.")
make_shirt('l','love')
#关键字调参
def make_shirt(size,words):
    print(f"the t-shirt size is {size},the words is {words}.")
make_shirt(size='m',words='china')

#8-4 大号T恤
def make_lshirt(size,words='i love python'):
    print(f"the size is {size},the words is {words}.")
make_lshirt(size='l')
make_lshirt(size='m')
make_lshirt(size='s',words='i love u')

#8-5 城市
def describe_city(city,country='china'):
    print(f"{city} is in {country}.")
describe_city(city='beijing')
describe_city('london','UK')

#8-6 城市名
def city_country(city,country):
    return city +','+country
print(city_country('hangzhou','china'))
print(city_country('xian','china'))

#8-7 专辑
def make_album(name,cd_name,cd_num = ''):
    album = {'歌手':name.title(),'专辑':cd_name.title()}
    if cd_num:
        album['歌曲数量'] = cd_num
    return album
value = make_album('michael jackson','billie jean')
print(value)
value = make_album('周杰伦','双截棍')
print(value)
value = make_album('张学友','smile','11')
print(value)
#8-8 用户的专辑
def make_album(name,cd_name):
    album = {'歌手':name.title(),'专辑':cd_name.title()}
    return album
while True:
    print('\n你对你喜欢的歌手有多了解?')
    print('我们来做个调查,关闭调查输入‘Q’')
    name = input('\n输入你喜欢的歌手名字')
    if name == 'Q':
        break
    cd_name = input('\n输入你知道的专辑名字')
    if cd_name == 'Q':
        break
    album_1 = make_album(name,cd_name)
    print('\n你喜欢的歌手信息如下,你觉得对吗?\n' + str(album_1))

#8-9 消息
def show_message(books):
    for book in books:
        message=f"to read book:{book.title()}"
        print(message)
read_books=['python','java']
show_message(read_books)

#8-10 发送消息
def show_messages(unread_books,read_books):
    while unread_books:
        toread_books=unread_books.pop()
        print(f"to read books {toread_books}")
        read_books.append(toread_books)
def send_messages(read_books):
    print('\n:These books have been read :')
    for book in read_books:
        print(book)
unread_books = ['python','java','c']
read_books = []
show_messages(unread_books,read_books)
send_messages(read_books)

#8-12 三明治
def sandwiches(*toppings):
    print("your sandwiches toppings:")
    for topping in toppings:
        print(f"{topping}")

sandwiches('tomato')
sandwiches('beef','meat')

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值