I'm currently reading Python Crash Course by Eric Matthes and I'm having an incredibly difficult time understanding chapter 8 which is all about functions. I am stuck on exercise 8-10 which asks me to use a new function to change a list used in the previous exercise.
Here is the exercise:
8-9. Magicians: 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.
8-10. Great Magicians: Start with a copy of your program from exercise 8-9. Write a function make_great() that modifies the list of magicians by adding the phrase the great to each magician's name. Call show_magicians() to see that the list has actually been modified.
Here is my code for 8-9:
def show_magicians(names):
"""Print each magician name"""
for name in names:
msg = name.title()
print(msg)
magician_names = ['sonic', 'tails', 'knuckles']
show_magicians(magician_names)
I've seen a very similar topic on this website and so I tried to use the code in the first answer on this page to help me out: Python crash course 8-10
However, my code still appears to be incorrect as the compiler prints 'the great' 3 times after each name.
Here is the current code I used for 8-10
def show_magicians(names):
"""Print each magician name"""
for name in names:
msg = name.title()
print(msg)
magician_names = ['sonic', 'tails', 'knuckles']
show_magicians(magician_names)
def make_great(list_magicians):
"""Add 'Great' to each name."""
for magician_name in magician_names:
for i in range(len(list_magicians)):
list_magicians[i] += " the great!"
make_great(magician_names)
show_magicians(magician_names)
I don't know why but it just seems that I've been struggling through out this entire chapter of functions. Does anyone by any chance have any recommended tutorials to take a look at to help me understand functions better? Thank you for your time.
make_great()you have nested for loops - how many times does the inner loop run?