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

 

📘 Python Crash Course – Chapter 8: Functions – Exercise 8-2 Solution

Welcome to another solution from Python Crash Course by Eric Matthes.


📝 Exercise 8-2

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

✅ Solution Code:


def favorite_book(title):
    print(f"One of my favorite books is {title.title()}.")
# Call the function with a book title
favorite_book("Alice in Wonderland")
favorite_book("To Kill a Mockingbird")

🧠 Code Explanation:

  1. Define the function with a parameter:

    The function favorite_book(title) is defined to accept one parameter, title. This parameter allows the function to receive a value when it is called.

  2. Use the parameter:

    Inside the function, the parameter title is used to display a message about a favorite book. The title() method is applied to ensure the book title is properly formatted with capitalized words.

  3. Call the function with arguments:

    The function is called twice with different arguments:

    • favorite_book("Alice in Wonderland"): Passes the string "Alice in Wonderland" as the argument for the title parameter.
    • favorite_book("To Kill a Mockingbird"): Passes the string "To Kill a Mockingbird" as the argument for the title parameter.

🔍 Output:


One of my favorite books is Alice In Wonderland.
One of my favorite books is To Kill A Mockingbird

📚 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