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

 

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

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


πŸ“ Exercise 8-4

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

✅ Solution Code:


def make_shirt(size="L", message="I love Python!"):
    print(f"The shirt size is {size} and the message printed on it is '{message}'.")

#Calling the Functions
make_shirt()
make_shirt(size="M")
make_shirt(size="S", message="Python is great!")
make_shirt(size="L")

🧠 Code Explanation:

  1. Define the function with default parameters:

    The function make_shirt(size="L", message="I love Python!") is defined with default values for its parameters:

    • size="L": The default size of the shirt is set to "L" (large).
    • message="I love Python!": The default message printed on the shirt is set to "I love Python!".
  2. Use the parameters:

    Inside the function, the parameters size and message are used to display the shirt's size and the message printed on it. If no arguments are provided when the function is called, the default values are used.

  3. Call the function with default values:

    The function is called without any arguments:

    • make_shirt(): Uses the default size "L" and the default message "I love Python!".
  4. Call the function with one overridden parameter:

    The function is called with the size parameter overridden:

    • make_shirt(size="M"): Changes the size to "M" (medium) while keeping the default message.
  5. Call the function with both parameters overridden:

    The function is called with both size and message parameters overridden:

    • make_shirt(size="S", message="Python is great!"): Changes the size to "S" (small) and the message to "Python is great!".

πŸ” 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 'I love Python!'.
The shirt size is S and the message printed on it is 'Python is great!'.       
The shirt size is L and the message printed on it is 'I love Python!'.

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