#8.1
def dispaly_message():
print("本章我们学习的是函数!")
dispaly_message()
输出:
本章我们学习的是函数!
#8.2
def favorite_book(title):
print("我最喜欢的一本书是:" + title)
favorite_book("三国演义")
输出:
我最喜欢的一本书是:三国演义
#8.3
def make_shirt(size,letters):
print("你要的是" + size + "码的衬衫,衬衫印着:" + letters)
make_shirt("m","我爱python哈")
make_shirt(letters="今天心情不错!",size="s")
输出:
你要的是m码的衬衫,衬衫印着:我爱python哈
你要的是s码的衬衫,衬衫印着:今天心情不错!
#8.4
def make_shirt(size,letters="I love Python"):
print("你要的是" + size + "码的衬衫,衬衫印着:" + letters)
make_shirt("大号")
make_shirt("中号")
make_shirt("中号",letters="今天心情不错")
输出:
你要的是大号码的衬衫,衬衫印着:I love Python
你要的是中号码的衬衫,衬衫印着:I love Python
你要的是中号码的衬衫,衬衫印着:今天心情不错
#8.5
def describe_city(city_name,country="中国"):
print(city_name + "属于" + country)
describe_city("北京")
describe_city("深圳")
describe_city("东京","日本")
输出:
北京属于中国
深圳属于中国
东京属于日本
#8.6
city_country("深圳","中国")
city_country("东京","日本")
city_country("纽约","美国")
输出:
深圳,中国
东京,日本
纽约,美国
#8.7
def make_album(singer_name,album_name,songs_num=""):
album = {"singer":singer_name,"album":album_name}
if songs_num:
album["songs"] = songs_num
return album
album1 = make_album("周杰伦","七里香",12)
album2 = make_album("SHE","不知道",10)
album3 = make_album("陈奕迅","不知道")
print(album1)
print(album2)
print(album3)
输出:
{'singer': '周杰伦', 'album': '七里香', 'songs': 12}
{'singer': 'SHE', 'album': '不知道', 'songs': 10}
{'singer': '陈奕迅', 'album': '不知道'}
#8.8
while True:
print("请输入专辑的歌手名字:")
print("(输入'q'退出程序)")
singer_n = input("singer name:")
if singer_n == "q":
break
album_n = input("album nanme:")
if album_n == "q":
break
album_dict = make_album(singer_n,album_n)
print(album_dict)
输出:
请输入专辑的歌手名字:
(输入'q'退出程序)
singer name:林俊杰
album nanme:不懂
{'singer': '林俊杰', 'album': '不懂'}
请输入专辑的歌手名字:
(输入'q'退出程序)
singer name:张韶涵
album nanme:欧若拉
{'singer': '张韶涵', 'album': '欧若拉'}
请输入专辑的歌手名字:
(输入'q'退出程序)
singer name:q
#8.9
def show_magicians(magicians):
print("下面这些全是大魔術師:")
for magician in magicians:
print("-" + magician)
big_magicians = ["大卫","董卿","刘谦"]
show_magicians(big_magicians)
输出:
下面这些全是大魔術師:
-大卫
-董卿
-刘谦
#8.10 8.11
def make_great(magicians):
return ["The Great" + magician for magician in magicians]
great_magicians = make_great(big_magicians[:])
show_magicians(great_magicians)
show_magicians(big_magicians)
输出:
下面这些全是大魔術師:
-The Great大卫
-The Great董卿
-The Great刘谦
下面这些全是大魔術師:
-大卫
-董卿
-刘谦
#8.12
def add_foods(foods):
print("您点的夹馍里有:")
for food in foods:
print(food + ";")
add_foods(["腊汁肉","辣子","黄瓜"])
add_foods(["鸡蛋","土豆丝","海蜇"])
add_foods(["腊汁肉","蘑菇","茄子"])
输出:
您点的夹馍里有:
腊汁肉;
辣子;
黄瓜;
您点的夹馍里有:
鸡蛋;
土豆丝;
海蜇;
您点的夹馍里有:
腊汁肉;
蘑菇;
茄子;
#8.13