π Python Crash Course – Chapter 8: Functions – Exercise 8-11 Solution
Welcome to another solution from Python Crash Course by Eric Matthes.
π Exercise 8-11
Task:
Start with your work from Exercise 8-10 . Call the
function make_great() with a copy of the list of magicians’ names .
Because the
original list will be unchanged, return the new list and store it in a separate list .
Call show_magicians() with each list to show that you have one list of the origi
nal names and one list with the Great added to each magician’s name
✅ Solution Code:
magicians = ["David Copperfield", "Harry Houdini", "Penn & Teller", "Dynamo", "Criss Angel"]
copy_magicians = magicians[:]
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(copy_magicians)
print("\nOriginal Magicians:")
show_magicians(magicians)
print("-----------------------------------------------------------")
print("Modified Magicians:")
show_magicians(copy_magicians)
π§ Code Explanation:
-
Create the original list:
The variable
magicians
is assigned a list containing the names of famous magicians, such as["David Copperfield", "Harry Houdini", "Penn & Teller", "Dynamo", "Criss Angel"]
. -
Create a copy of the list:
The variable
copy_magicians
is assigned a copy of themagicians
list using slicing (magicians[:]
). This ensures that changes made tocopy_magicians
do not affect the originalmagicians
list. -
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 a 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 the copied list:
The
make_great
function is called withcopy_magicians
as an argument. This modifies the copied list while leaving the originalmagicians
list unchanged. -
Display both lists:
The
show_magicians
function is called twice:- Once with the original
magicians
list to display the unmodified names. - Once with the modified
copy_magicians
list to display the updated names with'the Great Magician'
added.
- Once with the original
π Output:
Original Magicians:
David Copperfield
Harry Houdini
Penn & Teller
Dynamo
Criss Angel
-----------------------------------------------------------
Modified Magicians:
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