Resources for the second edition are here. I'd love to know what you think about Python Crash Course; please consider taking a brief survey. If you'd like to know when additional resources are available, you can sign up for email notifications here.
Solutions - Chapter 8
- 8-1: Message
- 8-2: Favorite Book
- 8-3: T-Shirt
- 8-4: Large Shirts
- 8-5: Cities
- 8-6: City Names
- 8-7: Album
- 8-8: User Albums
- 8-9: Magicians
- 8-10: Great Magicians
- 8-11: Unchanged Magicians
- 8-12: Sandwiches
- 8-14: Cars
- 8-15: Printing Models
Back to solutions.
8-1: Message
Write a function called display_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 I'm learning."""
msg = "I'm learning to store code in functions."
print(msg)
display_message()
Output:
I'm learning to store code in functions.
8-2: Favorite Book
Write a function called favorite_book()
that accepts one parameter, title
. The function should print a message, such as One of my favorite books is Alice in Wonderland.
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 someone's favorite book."""
print(title + " is one of my favorite books.")
favorite_book('The Abstract Wild')
Output:
The Abstract Wild is one of my favorite books.
8-3: T-Shirt
Write a function called make_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. Call the function a second time using keyword arguments.
def make_shirt(size, message):
"""Summarize the shirt that's going to be made."""
print("\nI'm 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:
I'm going to make a large t-shirt.
It will say, "I love Python!"
I'm going to make a medium t-shirt.
It will say, "Readability counts."
8-4: Large Shirts
Modify the make_shirt()
function so that shirts are large by default with a message that reads I love Python. Make a large shirt and a medium shirt with the default message, and a shirt of any size with a different message.
def make_shirt(size='large', message='I love Python!'):
"""Summarize the shirt that's going to be made."""
print("\nI'm going to make a " + size + " t-shirt.")
print('It will say, "' + message + '"')
make_shirt()
make_shirt(size='medium')
make_shirt('small', 'Programmers are loopy.')
Output:
I'm going to make a large t-shirt.
It will say, "I love Python!"
I'm going to make a medium t-shirt.
It will say, "I love Python!"
I'm going to make a small t-shirt.
It will say, "Programmers are loopy."
8-5: Cities
Write a function called describe_city()
that accepts the name of a city and its country. The function should print a simple sentence, such as Reykjavik 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.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 Names
Write a function called city_country()
that takes in the name of a city and its 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 that’s returned.
def city_country(city, country):
"""Return a string like 'Santiago, Chile'."""
return(city.title() + ", " + country.title())
city = city_country('santiago', 'chile')
print(city)
city = city_country('ushuaia', 'argentina')
print(city)
city = city_country('longyearbyen', 'svalbard')
print(city)
Output:
Santiago, Chile
Ushuaia, Argentina
Longyearbyen, Svalbard
8-7: Album
Write a function called make_album()
that builds a dictionary describing a music album. The 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 information correctly.
Add an optional parameter to make_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 album’s dictionary. Make at least one new function call that includes the nubmer of tracks on an album.
Simple version:
def make_album(artist, title):
"""Build a dictionary containing information about an album."""
album_dict = {
'artist': artist.title(),
'title': title.title(),
}
return album_dict
album = make_album('metallica', 'ride the lightning')
print(album)
album = make_album('beethoven', 'ninth symphony')
print(album)
album = make_album('willie nelson', 'red-headed stranger')
print(album)
Output:
{'title': 'Ride The Lightning', 'artist': 'Metallica'} {'title': 'Ninth Symphony', 'artist': 'Beethoven'} {'title': 'Red-Headed Stranger', 'artist': 'Willie Nelson'}
With tracks:
def make_album(artist, title, tracks=0):
"""Build a dictionary containing information about an album."""
album_dict = {
'artist': artist.title(),
'title': title.title(),
}
if tracks:
album_dict['tracks'] = tracks
return album_dict
album = make_album('metallica', 'ride the lightning')
print(album)
album = make_album('beethoven', 'ninth symphony')
print(album)
album = make_album('willie nelson', 'red-headed stranger')
print(album)
album = make_album('iron maiden', 'piece of mind', tracks=8)
print(album)
Output:
{'artist': 'Metallica', 'title': 'Ride The Lightning'} {'artist': 'Beethoven', 'title': 'Ninth Symphony'} {'artist': 'Willie Nelson', 'title': 'Red-Headed Stranger'} {'tracks': 8, 'artist': 'Iron Maiden', 'title': 'Piece Of Mind'}
8-8: User Albums
Start with your program from Exercise 8-7. Write a while
loop that allows users to enter an album’s artist and title. Once you have that information, call make_album()
with the user’s input and print the dictionary that’s created. Be sure to include a quit value in the while
loop.
def make_album(artist, title, tracks=0):
"""Build a dictionary containing information about an album."""
album_dict = {
'artist': artist.title(),
'title': title.title(),
}
if tracks:
album_dict['tracks'] = tracks
return album_dict
# Prepare the prompts.
title_prompt = "\nWhat album are you thinking of? "
artist_prompt = "Who's the artist? "
# Let the user know how to 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 thinking of? number of the beast Who's the artist? iron maiden {'artist': 'Iron Maiden', 'title': 'Number Of The Beast'} What album are you thinking of? touch of class Who's the artist? angel romero {'artist': 'Angel Romero', 'title': 'Touch Of Class'} What album are you thinking of? rust in peace Who's the artist? megadeth {'artist': 'Megadeth', 'title': 'Rust In Peace'} What album are you thinking of? quit Thanks for responding!
8-9: Magicians
Make a list of magician’s names. Pass the list to a function called show_magicians()
, wich prints the name of each magician in the list.
def show_magicians(magicians):
"""Print the name of each magician in the list."""
for magician in magicians:
print(magician.title())
magicians = ['harry houdini', 'david blaine', 'teller']
show_magicians(magicians)
Output:
Harry Houdini
David Blaine
Teller
8-10: Great Magicians
Start with a copy of your program from Exercise 8-9. Write a function called make_great()
that modifies the list of magicians by adding the phrase the Great to each magician’s name. Call show_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 each magician's 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 magicians.
for great_magician in great_magicians:
magicians.append(great_magician)
magicians = ['Harry Houdini', 'David Blaine', 'Teller']
show_magicians(magicians)
print("\n")
make_great(magicians)
show_magicians(magicians)
Output:
Harry Houdini
David Blaine
Teller
Teller the Great
David Blaine the Great
Harry Houdini the Great
8-11: Unchanged Magicians
Start with your work from Exercise 8-10. Call the function make_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. Call show_magicians()
with each list to show that you have one list of the original names and one list with the Great added to each magician’s 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 magician's 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 magicians.
for great_magician in great_magicians:
magicians.append(great_magician)
return magicians
magicians = ['Harry Houdini', 'David Blaine', 'Teller']
show_magicians(magicians)
print("\nGreat magicians:")
great_magicians = make_great(magicians[:])
show_magicians(great_magicians)
print("\nOriginal magicians:")
show_magicians(magicians)
Output:
Harry Houdini
David Blaine
Teller
Great magicians:
Teller the Great
David Blaine the Great
Harry Houdini the Great
Original magicians:
Harry Houdini
David Blaine
Teller
8-12: Sandwiches
Write a function that accepts a list of items a person wants on a sandwich. The function should have one parameter that collects as many items as the function call provides, and it should print a summary of the sandiwch that is being ordered. Call the function three tiems, using a different number of arguments each time.
def make_sandwich(*items):
"""Make a sandwich with the given items."""
print("\nI'll make you a great sandwich:")
for item in items:
print(" ...adding " + item + " to your sandwich.")
print("Your sandwich is ready!")
make_sandwich('roast beef', 'cheddar cheese', 'lettuce', 'honey dijon')
make_sandwich('turkey', 'apple slices', 'honey mustard')
make_sandwich('peanut butter', 'strawberry jam')
Output:
I'll make you a great sandwich:
...adding roast beef to your sandwich.
...adding cheddar cheese to your sandwich.
...adding lettuce to your sandwich.
...adding honey dijon to your sandwich.
Your sandwich is ready!
I'll make you a great sandwich:
...adding turkey to your sandwich.
...adding apple slices to your sandwich.
...adding honey mustard to your sandwich.
Your sandwich is ready!
I'll make you a great sandwich:
...adding peanut butter to your sandwich.
...adding strawberry jam to your sandwich.
Your sandwich is ready!
8-14: Cars
Write a function that stores information about a car in a dictionary. the function should always receive a manufacturer and a model name. It should then accept an arbitrary number of keyword arguments. Call the function with the required information and two other name-value pairs, such as a color or an optional feature. Your function should work for a call like this one:
car = make_car('subaru', 'outback', color='blue', tow_package=True)
Print the dictionary that’s returned to make sure all the information was stored correctly.
def make_car(manufacturer, model, **options):
"""Make a dictionary representing a car."""
car_dict = {
'manufacturer': manufacturer.title(),
'model': model.title(),
}
for option, value in options.items():
car_dict[option] = value
return car_dict
my_outback = make_car('subaru', 'outback', color='blue', tow_package=True)
print(my_outback)
my_accord = make_car('honda', 'accord', year=1991, color='white',
headlights='popup')
print(my_accord)
Output:
{'manufacturer': 'Subaru', 'color': 'blue', 'tow_package': True, 'model': 'Outback'} {'year': 1991, 'manufacturer': 'Honda', 'color': 'white', 'headlights': 'popup', 'model': 'Accord'}
8-15: Printing Models
Put the functions for the example printing_models.py in a separate file called printing_functions.py. Write an import
statement at the top of printing_models.py, and modify the file to use the imported functions.
Note: The text refers to print_models.py, but it should say printing_models.py.
printing_functions.py:
"""Functions related to printing 3d models."""
def print_models(unprinted_designs, completed_models):
"""
Simulate printing each design, until there are none left.
Move each design to completed_models after printing.
"""
while unprinted_designs:
current_design = unprinted_designs.pop()
# Simulate creating a 3d print from the design.
print("Printing model: " + current_design)
completed_models.append(current_design)
def show_completed_models(completed_models):
"""Show all the models that were printed."""
print("\nThe following models have been printed:")
for completed_model in completed_models:
print(completed_model)
printing_models.py:
import printing_functions as pf
unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
completed_models = []
pf.print_models(unprinted_designs, completed_models)
pf.show_completed_models(completed_models)
Output:
Printing model: dodecahedron
Printing model: robot pendant
Printing model: iphone case
The following models have been printed:
dodecahedron
robot pendant
iphone case