Exercise 8-7 Solution Python Crash Course Chapter 8 : Functions

 

📘 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:

  1. 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 to None.
  2. Create a dictionary:

    Inside the function, a dictionary named album is created with keys 'artist' and 'title', storing the values passed to the function.

  3. Handle the optional parameter:

    If the tracks parameter is provided (i.e., it is not None), a new key-value pair 'tracks' is added to the dictionary to store the number of tracks.

  4. Return the dictionary:

    The function returns the album dictionary containing the album's details.

  5. 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.
  6. 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:


🔗 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

Previous Post Next Post

Contact Form