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