ImageVerifierCode 换一换
格式:DOCX , 页数:14 ,大小:19.09KB ,
资源ID:10250987      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/10250987.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(SolutionsChapter 8.docx)为本站会员(b****7)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

SolutionsChapter 8.docx

1、Solutions Chapter 8Solutions - Chapter 88-1: MessageWrite a function calleddisplay_message()that prints one sentence telling everyone what you are learning about in this chapter. Call the function, and make sure the message displays correctly.def display_message(): Display a message about what Im le

2、arning. msg = Im learning to store code in functions. print(msg)display_message()Output:Im learning to store code in functions.8-2: Favorite BookWrite a function calledfavorite_book()that accepts one parameter,title. The function should print a message, such asOne of my favorite books is Alice in Wo

3、nderland.Call the function, making sure to include a book title as an argument in the function call.def favorite_book(title): Display a message about someones favorite book. print(title + is one of my favorite books.)favorite_book(The Abstract Wild)Output:The Abstract Wild is one of my favorite book

4、s.8-3: T-ShirtWrite a function calledmake_shirt()that accepts a size and the text of a message that should be printed on the shirt. The function should print a sentence summarizing the size of the shirt and the message printed on it.Call the function once using positional arguments to make a shirt.

5、Call the function a second time using keyword arguments.def make_shirt(size, message): Summarize the shirt thats going to be made. print(nIm going to make a + size + t-shirt.) print(It will say, + message + )make_shirt(large, I love Python!)make_shirt(message=Readability counts., size=medium)Output:

6、Im going to make a large t-shirt.It will say, I love Python!Im going to make a medium t-shirt.It will say, Readability counts.8-4: Large ShirtsModify themake_shirt()function so that shirts are large by default with a message that readsI love Python. Make a large shirt and a medium shirt with the def

7、ault message, and a shirt of any size with a different message.def make_shirt(size=large, message=I love Python!): Summarize the shirt thats going to be made. print(nIm going to make a + size + t-shirt.) print(It will say, + message + )make_shirt()make_shirt(size=medium)make_shirt(small, Programmers

8、 are loopy.)Output:Im going to make a large t-shirt.It will say, I love Python!Im going to make a medium t-shirt.It will say, I love Python!Im going to make a small t-shirt.It will say, Programmers are loopy.8-5: CitiesWrite a function calleddescribe_city()that accepts the name of a city and its cou

9、ntry. The function should print a simple sentence, such asReykjavik is in Iceland.Give the parameter for the country a default value. Call your function for three different cities, at least one of which is not in the default country.def describe_city(city, country=chile): Describe a city. msg = city

10、.title() + is in + country.title() + . print(msg)describe_city(santiago)describe_city(reykjavik, iceland)describe_city(punta arenas)Output:Santiago is in Chile.Reykjavik is in Iceland.Punta Arenas is in Chile.8-6: City NamesWrite a function calledcity_country()that takes in the name of a city and it

11、s country. The function should return a string formatted like this:“Santiago, Chile”Call your function with at least three city-country pairs, and print the value thats returned.def city_country(city, country): Return a string like Santiago, Chile. return(city.title() + , + country.title()city = cit

12、y_country(santiago, chile)print(city)city = city_country(ushuaia, argentina)print(city)city = city_country(longyearbyen, svalbard)print(city)Output:Santiago, ChileUshuaia, ArgentinaLongyearbyen, Svalbard8-7: AlbumWrite a function calledmake_album()that builds a dictionary describing a music album. T

13、he function should take in an artist name and an album title, and it should return a dictionary containing these two pieces of information. Use the function to make three dictionaries representing different albums. Print each return value to show that the dictionaries are storing the album informati

14、on correctly.Add an optional parameter tomake_album()that allows you to store the nubmer of tracks on an album. If the calling line includes a value for the number of tracks, add that value to the albums dictionary. Make at least one new function call that includes the nubmer of tracks on an album.S

15、imple version:def make_album(artist, title): Build a dictionary containing information about an album. album_dict = artist: artist.title(), title: title.title(), return album_dictalbum = make_album(metallica, ride the lightning)print(album)album = make_album(beethoven, ninth symphony)print(album)alb

16、um = make_album(willie nelson, red-headed stranger)print(album)Output:title: Ride The Lightning, artist: Metallicatitle: Ninth Symphony, artist: Beethoventitle: Red-Headed Stranger, artist: Willie NelsonWith tracks:def make_album(artist, title, tracks=0): Build a dictionary containing information ab

17、out an album. album_dict = artist: artist.title(), title: title.title(), if tracks: album_dicttracks = tracks return album_dictalbum = make_album(metallica, ride the lightning)print(album)album = make_album(beethoven, ninth symphony)print(album)album = make_album(willie nelson, red-headed stranger)p

18、rint(album)album = make_album(iron maiden, piece of mind, tracks=8)print(album)Output:artist: Metallica, title: Ride The Lightningartist: Beethoven, title: Ninth Symphonyartist: Willie Nelson, title: Red-Headed Strangertracks: 8, artist: Iron Maiden, title: Piece Of Mind8-8: User AlbumsStart with yo

19、ur program from Exercise 8-7. Write awhileloop that allows users to enter an albums artist and title. Once you have that information, callmake_album()with the users input and print the dictionary thats created. Be sure to include a quit value in thewhileloop.def make_album(artist, title, tracks=0):

20、Build a dictionary containing information about an album. album_dict = artist: artist.title(), title: title.title(), if tracks: album_dicttracks = tracks return album_dict# Prepare the prompts.title_prompt = nWhat album are you thinking of? artist_prompt = Whos the artist? # Let the user know how to

21、 quit.print(Enter quit at any time to stop.)while True: title = input(title_prompt) if title = quit: break artist = input(artist_prompt) if artist = quit: break album = make_album(artist, title) print(album)print(nThanks for responding!)Output:Enter quit at any time to stop.What album are you thinki

22、ng of? number of the beastWhos the artist? iron maidenartist: Iron Maiden, title: Number Of The BeastWhat album are you thinking of? touch of classWhos the artist? angel romeroartist: Angel Romero, title: Touch Of ClassWhat album are you thinking of? rust in peaceWhos the artist? megadethartist: Meg

23、adeth, title: Rust In PeaceWhat album are you thinking of? quitThanks for responding!8-9: MagiciansMake a list of magicians names. Pass the list to a function calledshow_magicians(), wich prints the name of each magician in the list.def show_magicians(magicians): Print the name of each magician in t

24、he list. for magician in magicians: print(magician.title()magicians = harry houdini, david blaine, tellershow_magicians(magicians)Output:Harry HoudiniDavid BlaineTeller8-10: Great MagiciansStart with a copy of your program from Exercise 8-9. Write a function calledmake_great()that modifies the list

25、of magicians by adding the phrasethe Greatto each magicians name. Callshow_magicians()to see that the list has actually been modified.def show_magicians(magicians): Print the name of each magician in the list. for magician in magicians: print(magician)def make_great(magicians): Add the Great! to eac

26、h magicians name. # Build a new list to hold the great musicians. great_magicians = # Make each magician great, and add it to great_magicians. while magicians: magician = magicians.pop() great_magician = magician + the Great great_magicians.append(great_magician) # Add the great magicians back into

27、magicians. for great_magician in great_magicians: magicians.append(great_magician)magicians = Harry Houdini, David Blaine, Tellershow_magicians(magicians)print(n)make_great(magicians)show_magicians(magicians)Output:Harry HoudiniDavid BlaineTellerTeller the GreatDavid Blaine the GreatHarry Houdini th

28、e Great8-11: Unchanged MagiciansStart with your work from Exercise 8-10. Call the functionmake_great()with a copy of the list of magicians names. Because the original list will be unchanged, return the new list and store it in a separate list. Callshow_magicians()with each list to show that you have

29、 one list of the original names and one list withthe Greatadded to each magicians name.def show_magicians(magicians): Print the name of each magician in the list. for magician in magicians: print(magician)def make_great(magicians): Add the Great! to each magicians name. # Build a new list to hold th

30、e great musicians. great_magicians = # Make each magician great, and add it to great_magicians. while magicians: magician = magicians.pop() great_magician = magician + the Great great_magicians.append(great_magician) # Add the great magicians back into magicians. for great_magician in great_magicians: magicians.append(great_magician) return magiciansmagicians = Harry Houdini

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1