π 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:
-
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!".
-
Use the parameters:
Inside the function, the parameters
size
andmessage
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. -
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!".
-
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.
-
Call the function with both parameters overridden:
The function is called with both
size
andmessage
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:
- ➤ 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