I'm teaching myself Python out of a book and I am having trouble with the 2nd part of this 2 part exercise.
The 1st part of the exercise: 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.
I was able to do this part with no problem
My code for the 1st part:
magicians_names = ['Marv', 'Wowzo', 'Trickster', 'Didlo']
def show_magicians():
for name in magicians_names:
print(name)
show_magicians()
The 2nd part of the exercise: 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.
My code for the 2nd part
magicians_names = ['Marv', 'Wowzo', 'Trickster', 'Didlo']
def show_magicians():
for name in magicians_names:
print(name)
def make_great():
show_magicians()
I've tried just about every idea I can think of for the make_great function but nothing has worked so far. Any ideas or examples would be greatly appreciated.
Each item in a list has an associated index, starting from zero. As you probably know, you can access the items in the list using those indexes:
>>> magicians_names = ['Marv', 'Wowzo', 'Trickster', 'Didlo']
>>> magicians_names[0]
'Marv'
You can also modify the list items using those indexes:
>>> magicians_names[0] = 'Jerry Boomfang'
>>> magicians_names[0]
'Jerry Boomfang'
So what you need to do is loop through both the list and its indexes, modifying as you go. Which is exactly what the enumerate function is for.
>>> for index, magician in enumerate(magicians_names):
... magicians_names[index] += ' is great!'
...
>>> magicians_names
['Jerry Boomfang is great!', 'Wowzo is great!', 'Trickster is great!', 'Didlo is great!']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With