π Python Crash Course – Chapter 8: Functions – Exercise 8-7 Solution
Welcome to another solution from Python Crash Course by Eric Matthes.
π Exercise 8-7
Task:
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
number of tracks on an album . If the calling line includes a value for the num
ber of tracks, add that value to the album’s dictionary .
Make at least one new
function call that includes the number of tracks on an album
✅ Solution Code:
def make_album(artist, title, tracks=None):
album = {
'artist': artist,
'title': title
}
if tracks:
album['tracks'] = tracks
return album
# Create three dictionaries representing different albums
album1 = make_album("Taylor Swift", "1989")
album2 = make_album("Ed Sheeran", "Divide", 12)
album3 = make_album("Adele", "25")
# Print each return value to show that the dictionaries are storing the album information correctly
print(album1)
print(album2)
print(album3)
π§ Code Explanation:
-
Define the function:
The function
make_album(artist, title, tracks=None)
is defined with three parameters:artist
: Represents the name of the artist.title
: Represents the title of the album.tracks=None
: An optional parameter that allows the user to specify the number of tracks on the album. If no value is provided, it defaults toNone
.
-
Create a dictionary:
Inside the function, a dictionary named
album
is created with keys'artist'
and'title'
, storing the values passed to the function. -
Handle the optional parameter:
If the
tracks
parameter is provided (i.e., it is notNone
), a new key-value pair'tracks'
is added to the dictionary to store the number of tracks. -
Return the dictionary:
The function returns the
album
dictionary containing the album's details. -
Create album dictionaries:
The function is called three times with different arguments to create dictionaries for three albums:
make_album("Taylor Swift", "1989")
: Creates an album dictionary without specifying the number of tracks.make_album("Ed Sheeran", "Divide", 12)
: Creates an album dictionary and includes the number of tracks (12).make_album("Adele", "25")
: Creates an album dictionary without specifying the number of tracks.
-
Display the dictionaries:
The returned dictionaries are displayed to verify that they correctly store the album information.
π Output:
{'artist': 'Taylor Swift', 'title': '1989'}
{'artist': 'Ed Sheeran', 'title': 'Divide', 'tracks': 12}
{'artist': 'Adele', 'title': '25'}
π Related Exercises from Chapter 8:
- ➤ Exercise 8-1 | Message
- ➤ Exercise 8-2 | Favorite Book
- ➤ Exercise 8-3 | T-Shirt
- ➤ Exercise 8-4 | Large Shirts
- ➤ Exercise 8-5 | Cities
- ➤ Exercise 8-6 | City Names
- ➤ Exercise 8-7 | Album
- ➤ Exercise 8-8 | User Albums
- ➤ Exercise 8-9 | Magicians
- ➤ Exercise 8-10 | Great Magicians
- ➤ Exercise 8-11 | Unchanged Magicians
- ➤ Exercise 8-12 | Sandwiches
- ➤ Exercise 8-13 | User Profile
- ➤ Exercise 8-14 | Cars
- ➤ Exercise 8-15 | Printing Models
- ➤ Exercise 8-16 | Imports
- ➤ Exercise 8-17 | Styling Functions
π Connect With Us:
“In the world of code, Python is the language of simplicity, where logic meets creativity, and every line brings us closer to our goals.”
— Only Python
π Tags: #Python #PythonCrashCourse #CodingForBeginners #LearnPython #PythonProjects #PythonTips
πTrending Topics
π Connect With Us:
“In the world of code, Python is the language of simplicity, where logic meets creativity, and every line brings us closer to our goals.”— Only Python
π Follow Us And Stay Updated For Daily Updates
Comments
Post a Comment