Exercise 8-11 Solution Python Crash Course Chapter 8 : Functions

 

πŸ“˜ 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:

  1. 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"].

  2. Create a copy of the list:

    The variable copy_magicians is assigned a copy of the magicians list using slicing (magicians[:]). This ensures that changes made to copy_magicians do not affect the original magicians list.

  3. 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.

  4. 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 the range() 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.
  5. Modify the copied list:

    The make_great function is called with copy_magicians as an argument. This modifies the copied list while leaving the original magicians list unchanged.

  6. 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.

πŸ” 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:


πŸ”— 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

“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