Python编程:从入门到实践(课后习题8)

来源:互联网 发布:ep软件 编辑:程序博客网 时间:2024/05/17 23:55
# 8-1 消息def display_message():print("This chapter I will learning function.")display_message()# 8-2 喜欢的图书def favorite_book(title):print("One of my favorite books is " + title + ".")favorite_book('Python for Data Analysis')# 8-3 T恤def make_shirt(size, word):print("T-shirt's size is: " + str(size) + '.')print("T-shirt's word is: " + str(word) + '.')make_shirt(L, 24)make_shirt(word='I love you', size='M')# 8-4 大号T恤def make_shirt(size='大号', word='I love Python.'):print("T-shirt's size is: " + str(size) + '.')print("T-shirt's word is: " + str(word) + '.')make_shirt()make_shirt(size='中号')make_shirt(size='加大号', word="I don't care")# 8-5 城市def describe_city(city, nation='China'):print("City: " + city)print("Nation: " + nation)describe_city('Guangzhou')describe_city('Beijing')describe_city('Los angels', nation='America')# 8-6 城市名def city_country(city, nation):x = city + ', ' + nationreturn xcity_nation1 = city_country('Guangzhou', 'China')city_nation2 = city_country('Tokyo', 'Japan')city_nation3 = city_country('Seoul', 'Korea')print(city_nation1)print(city_nation2)print(city_nation3)# 8-7 专辑def make_album(singer, album, num=''):albums = {}if num:albums['singer'] = singeralbums['album'] = albumalbums['num'] = numelse:albums['singer'] = singeralbums['album'] = albumreturn albumsu87 = make_album('陈奕迅', 'U87')u87_num = make_album('陈奕迅', 'U87', num=12)happy = make_album('陈奕迅', '我的快乐时代', num=15)print(u87)print(u87_num)print(happy)# 8-8 用户的专辑album_active = Truewhile album_active:singer = input("Singer: ")album = input("Album: ")num = input("Number: ")you_album = make_album(singer, album, num)print(you_album)quit = input("Continue or quit? (y/n) ")if quit == 'n':album_active = False# 8-9 魔术师magician = ['criss', 'jason', 'cyril']def show_magicians(magician_list):for magician in magician_list:print(magician.title())show_magicians(magician)# 8-10 了不起的魔术师def make_great(magician_list):for i in range(3):magician_list[i] = 'the Great ' + magician_list[i]return magician_list  # 差点忘记return了make_great(magician)show_magicians(magician)# 8-11 不变的魔术师magician_great = make_great(magician[:])show_magicians(magician)show_magicians(magician_great)# 8-12 三明治def sandwich(*args):for i in args:print(i)# 8-13 用户简介def build_profile(first, last, **user_info):profile = {}profile['first_name'] = firstprofile['last_name'] = lastfor key, value in user_info.items():profile[key] = valuereturn profilemy_profile = build_profile('Zhou', 'Kai', user_info={'age': 26, 'hobby': 'basketball', 'lover': 'Zhanglili'})print(my_profile)# 8-14 汽车def car_information(maker, type1, **kw):information = {}information['maker'] = makerinformation['type'] = type1for i, j in kw.items():information[i] = jreturn information

阅读全文
0 0
原创粉丝点击