π 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:
-
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.
-
Use the parameters:
Inside the function, the parameters
size
andmessage
are used to display a summary of the shirt's size and the message printed on it. -
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.
-
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:
- ➤ 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