《Python编程-从入门到实践》课后习题(8)

8-1

def display_message():
    print('Function in python')
deisplay_message()

8-2

def favorite_book(title):
    print('One of my favorite books is '+title)
favorite_book('A Brief History of Time')

8-3

def make_shirt(size, word):
    print('Your shirt is size '+size+' with "'+word+'" ')
make_shirt('S', 'You bet')
make_shirt(size='S', word='Bite me')

8-4

def make_shirt(size, word='I love Python'):
    print('Your shirt is size '+size+' with "'+word+'" ')
make_shirt('L')
make_shirt('M')
make_shirt('S', 'I hate Python')

8-5

def describe_city(city='Reykjavik', country='Iceland'):
    print(city+' is in '+country)
describe_city()
describe_city('York', 'U.K.')
describe_city('New York', 'U.S.')   

8-6

def city_country(city, country):
    return city+', '+country
print(city_country('H.K.', 'P.R.C.'))
print(city_country('York', 'U.K.'))
print(city_country('New York', 'U.S.'))

8-7

def make_album(singer, album, count=0):
    if count:
        return {singer: album, 'count': count}
    return {singer: album}
print(make_album('Pheobe', 'Smelly Cat'))
print(make_album('A', 'B'))
print(make_album('C', 'D'))
print(make_album('E', 'F', 10))

8-8

def make_album(singer, album, count=0):
    if count:
        return {singer: album, 'count': count}
    return {singer: album}
while True:
    singer = input('Singer: ')
    if singer=='quit':
        break
    album = input('Album: ')
    if album=='quit':
        break
    print(make_album(singer, album))

8-9

def show_magicians(magicians):
    for magician in magicians:
        print(magician)
magicians = ['A', 'B', 'C']
show_magicians(magicians)

8-10

def show_magicians(magicians):
    for magician in magicians:
        print(magician)
def make_great(magicians):
    for i in range(0, len(magicians)):
        magicians[i] = 'The Great '+magicians[i]
magicians = ['A', 'B', 'C']
make_great(magicians)
show_magicians(magicians)

8-11

def show_magicians(magicians):
    for magician in magicians:
        print(magician)
def make_great(magicians):
    for i in range(0, len(magicians)):
        magicians[i] = 'The Great '+magicians[i]
    return magicians
magicians = ['A', 'B', 'C']
changed = make_great(magicians[:])
show_magicians(magicians)
show_magicians(changed)

8-12

def sandwich(*materials):
    print('You sandwich inludes: ', end='')
    for material in materials:
        print(material, end=' ')
    print()
sandwich('A', 'B', 'C')
sandwich('A', 'B')
sandwich('A')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值