π Python Crash Course – Chapter 8: Functions – Exercise 8-9 Solution
Welcome to another solution from Python Crash Course by Eric Matthes.
π Exercise 8-9
Task:
Make a list of magician’s names . Pass the list to a function
called show_magicians(), which prints the name of each magician in the list
✅ Solution Code:
# List of magician's names
magicians = ["David Copperfield", "Harry Houdini", "Penn & Teller", "Dynamo", "Criss Angel"]
def show_magicians(magicians_list):
for names in magicians_list:
print(names)
# Call the function to show the names of the magicians
show_magicians(magicians)
π§ Code Explanation:
-
Create a list of magicians:
The variable
magicians
is assigned a list containing the names of famous magicians, such as["David Copperfield", "Harry Houdini", "Penn & Teller", "Dynamo", "Criss Angel"]
. -
Define the function:
The function
show_magicians(magicians_list)
is defined to accept one parameter:magicians_list
: Represents the list of magician names passed to the function.
-
Iterate through the list:
Inside the function, a
for
loop is used to iterate through each element in themagicians_list
. The variablenames
takes on the value of each magician's name during each iteration. -
Display each name:
For each iteration, the name of the magician is displayed. This ensures that all names in the list are processed and shown.
-
Call the function:
The function
show_magicians()
is called with themagicians
list as an argument. This passes the list of magician names to the function for processing.
π Output:
David Copperfield
Harry Houdini
Penn & Teller
Dynamo
Criss Angel
π 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