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

 

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

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

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

  3. 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 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.
  4. Modify and display the list:

    The make_great function is called with the magicians list as an argument, modifying the list in place. The show_magicians function is then called to display the updated list of magician names.

  5. Verify the modified list:

    The modified magicians list is displayed to confirm that the changes made by the make_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:


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