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

 

πŸ“˜ Python Crash Course – Chapter 8: Functions – Exercise 8-3 Solution

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


πŸ“ Exercise 8-3

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

✅ Solution Code:


    print(f"The shirt size is {size} and the message printed on it is '{message}'.")

# Call the function using positional arguments
make_shirt("L", "I love Python!")
# Call the function using keyword arguments
make_shirt(message="Python is awesome!", size="M")

🧠 Code Explanation:

  1. Define the function with parameters:

    The function make_shirt(size, message) is defined to accept two parameters:

    • size: Represents the size of the shirt.
    • message: Represents the text to be printed on the shirt.
  2. Use the parameters:

    Inside the function, the parameters size and message are used to display a summary of the shirt's size and the message printed on it.

  3. Call the function using positional arguments:

    The function is called with arguments in the same order as the parameters:

    • make_shirt("L", "I love Python!"): Passes "L" as the size and "I love Python!" as the message.
  4. Call the function using keyword arguments:

    The function is called with arguments specified by their parameter names, allowing the order to be changed:

    • make_shirt(message="Python is awesome!", size="M"): Passes "M" as the size and "Python is awesome!" as the message.

πŸ” Output:


The shirt size is L and the message printed on it is 'I love Python!'.
The shirt size is M and the message printed on it is 'Python is awesome!'.

πŸ“š 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

“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