π Python Crash Course – Chapter 8: Functions – Exercise 8-10 Solution
Welcome to another solution from Python Crash Course by Eric Matthes.
π Exercise 8-10
Task:
Start with a copy of your program from Exercise 8-9 .
Write a function called make_great() that modifies the list of magicians by add
ing the phrase the Great to each magician’s name .
Call show_magicians() to
see that the list has actually been modified
✅ Solution Code:
# List of magician's names
magicians = ["David Copperfield", "Harry Houdini", "Penn & Teller", "Dynamo", "Criss Angel"]
def show_magicians(magicians_list):
"""Print the names of the magicians in the list."""
for names in magicians_list:
print(names)
def make_great(magicians_list):
"""Modify the list of magicians by adding 'the Great' to each name."""
for i in range(len(magicians_list)):
magicians_list[i] = f"{magicians_list[i]} the Great Magician"
return magicians_list
make_great(magicians)
show_magicians(magicians)
print(f' magician list: {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
show_magicians
function:This function accepts a list of magician names as a parameter and iterates through the list using a
for
loop. Each name in the list is displayed to confirm the current state of the list. -
Define the
make_great
function:This function modifies the list of magician names by appending the phrase
'the Great Magician'
to each name. Here's how it works:- A
for
loop iterates through the list using therange()
function to access each index. - Each name in the list is updated by concatenating the original name with the phrase
'the Great Magician'
. - The modified list is returned to confirm the changes.
- A
-
Modify and display the list:
The
make_great
function is called with themagicians
list as an argument, modifying the list in place. Theshow_magicians
function is then called to display the updated list of magician names. -
Verify the modified list:
The modified
magicians
list is displayed to confirm that the changes made by themake_great
function are persistent.
π Output:
David Copperfield the Great Magician
Harry Houdini the Great Magician
Penn & Teller the Great Magician
Dynamo the Great Magician
Criss Angel the Great Magician
magician list: ['David Copperfield the Great Magician', 'Harry Houdini the Great Magician', 'Penn & Teller the Great Magician', 'Dynamo the Great Magician',
'Criss Angel the Great Magician']
π 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