8-2 喜欢的图书
def favourite_book(title):
print("One of my favourite books is "+title)
favourite_book("Alice in Wonderland")
输出结果是:
One of my favourite books is Alice in Wonderland
8-3,8-4 T恤
def make_shirt(size = '大号',message = 'Python'):
print('制作一件印有字样"I love '+message+'"的'+size+' T-shirt')
make_shirt()
make_shirt("中号")
make_shirt(message="C++",size="小号")
输出结果是:
制作一件印有字样"I love Python"的大号T-shirt
制作一件印有字样"I love Python"的中号T-shirt
制作一件印有字样"I love C++"的小号T-shirt
8-6 城市名
def city_country(city,country):
return '"'+city.title()+', '+country.title()+'"'
print(city_country("santiago","chile"))
print(city_country("moscow","russia"))
print(city_country("Washington D.C.","america"))
输出结果是:
"Santiago, Chile"
"Moscow, Russia"
"Washington D.C., America"
8-9,8-10 伟大的魔术师
def show_magicians(lists):
print(lists)
def make_great(lists):
for i in range(len(lists)):
lists[i] = "the Great "+lists[i]
list = ["Dynamo","Jason Latimer","刘谦"]
show_magicians(list)
make_great(list)
show_magicians(list)
输出结果是:
['Dynamo', 'Jason Latimer', '刘谦']
['the Great Dynamo', 'the Great Jason Latimer', 'the Great 刘谦']
8-11 不变的魔术师
def show_magicians(lists):
print(lists)
def make_great(lists):
for i in range(len(lists)):
lists[i] = "the Great "+lists[i]
show_magicians(lists)
list = ["Dynamo","Jason Latimer","刘谦"]
show_magicians(list)
make_great(list[:])
show_magicians(list)
输出结果是:
['Dynamo', 'Jason Latimer', '刘谦']
['the Great Dynamo', 'the Great Jason Latimer', 'the Great 刘谦']
['Dynamo', 'Jason Latimer', '刘谦']
8-14 汽车
def make_car (manufacturer,model,**other):
profile = {}
profile['manufacturer']=manufacturer
profile['model']=model
for k,v in other.items():
profile[k]=v
return profile
car = make_car('saburu','outbreak',color = 'blue',tow_package = True)
print(car)
输出结果是:
{'manufacturer': 'saburu', 'model': 'outbreak',
'color': 'blue', 'tow_package': True}