some answer for chapter 8

8-2

def favorite_message(title ):

    print("One of myfavorite books is "+title.title()+".")

favorite_message("SophiesWorld")

output:

One of my favorite books is Sophies World.


8-4

def make_shirt(word = 'I love Python',size='small'):

    print('A '+ size +' size shirtwith the word '+"'"+word+"' has beenmade." )

make_shirt(size='big')

make_shirt(size='normal')

make_shirt(word = 'X')

output:

A big size shirt with the word 'I lovePython' has been made.

A normal size shirt with the word 'I lovePython' has been made.

A small size shirt with the word 'X' has beenmade.


8-6

def city_country(city,country):

    out=city+','+country

    return out

print(city_country('Beijing','China'))

print(city_country('Guangdong','China'))

print(city_country('London','America'))

output:

Beijing,China

Guangdong,China

London,America


8-8

def make_album(singer,album,num= -1):

    out={singer:album}

    if num!=-1:

        out['num']=num

    return out

while True:

    flag = input("input 'q'to quit ,'c' to make albums:")

    if flag == 'q':

        break

    singer = input("what's thesinger's name?")

    album = input("what's thealbum's name?")

    num = input("How manysongs are in it?")

    print(make_album(singer,album,num))

output:

input 'q' to quit ,'c' to make albums:c

what's the singer's name?a

what's the album's name?album

How many songs are in it?20

{'a': 'album', 'num': '20'}

input 'q' to quit ,'c' to make albums:q


8-10

def show_magicans(mans):

    print("Theyare:")

    for magican in mans:

        print(magican)

mans = ['a','b','c']

def make_great(mans):

    mans2=[]

    while mans:

        mans2.append( 'Great '+ mans.pop())

    while mans2:

        mans.append(mans2.pop())

make_great(mans)

show_magicans(mans)

output:

They are:

Great a

Great b

Great c

8-11

mans = ['a','b','c']

def make_great(mans):

    mans2=[]

    for magican in mans:

        mans2.append('Great '+ magican)

    return mans2

mans2 = make_great(mans)

print('the initial one:')

show_magicans(mans)

print('the new one:')

show_magicans(mans2)

output:

the initial one:

They are:

a

b

c

the new one:

They are:

Great a

Great b

Great c

 


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值