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