8-2喜欢的图书
def favorite_book(title):
print("One of my favorite book is "+title+".")
favorite_book("Alice in Wonderland")
8-3T恤
def make_shirt(size,word):
print("The size of the T-shirt is "+size+" and the word of the T-shirt is "+word+".")
s=input("Please input the size of the T-shirt.\n")
w=input("Please input the word of the T-shirt.\n")
make_shirt(s,w)
make_shirt(word=w,size=s)
8-4大号T恤
def make_shirt(size='L',word='I love Python'):
print("The size of the T-shirt is "+size+" and the word of the T-shirt is "+word+".")
make_shirt()
make_shirt("M")
make_shirt("XL","Happy!")
8-7专辑
def make_album(singer,album,num=0):
if(num):
return {'singer':singer,'album':album,'num':num}
else:
return {'singer':singer,'album':album}
album1=make_album('Alice','Sun',5)
album2=make_album('Bob','Wide')
album3=make_album('Lily','Quiet',4)
print(album1)
print(album2)
print(album3)
8-8用户的专辑
def make_album(singer,album,num=0):
if(num):
return {'singer':singer,'album':album,'num':num}
else:
return {'singer':singer,'album':album}
album1=make_album('Alice','Sun',5)
album2=make_album('Bob','Wide')
album3=make_album('Lily','Quiet',4)
print(album1)
print(album2)
print(album3)
flag=True
while flag:
singer=input("Please input the singer name(input E for exit)\n")
if(singer=='E'):
break
album=input("Please input the albunm name\n")
print(make_album(singer,album))
8-9魔术师
def show_magicians(magicians):
for magician in magicians:
print(magician)
magicians=['Alice','Bob','Cindy','David','Lily']
show_magicians(magicians)
8-10了不起的魔术师
def show_magicians(magicians):
for magician in magicians:
print(magician)
def make_great(magicians):
num=len(magicians)
i=0
while i<num:
magicians[i]='the Great '+magicians[i]
i=i+1
magicians=['Alice','Bob','Cindy','David','Lily']
show_magicians(magicians)
make_great(magicians)
print('\n')
show_magicians(magicians)
8-11不变的魔术师
def show_magicians(magicians):
for magician in magicians:
print(magician)
def make_great(magicians):
num=len(magicians)
i=0
while i<num:
magicians[i]='the Great '+magicians[i]
i=i+1
return magicians
magicians=['Alice','Bob','Cindy','David','Lily']
show_magicians(magicians)
new_magicians=make_great(magicians[:])
print('\n')
show_magicians(new_magicians)
print('\n')
show_magicians(magicians)
8-14汽车
def make_car(manufacturer,type,**keys):
car={"manufacturer":manufacturer,"type":type}
for key,value in keys.items():
car[key]=value
print(car)
car = make_car('subaru', 'outback', color='blue', tow_package=True)