Intensive python course by Eric Matthes ex8.10 [closed]

2

I'm having a hard time doing an exercise in the book 'Intensive python course by Eric Matthes' the exercises is as follows:

8.10 - Great Magicians: Eat with a copy of your exercise program 8.9. Write a function called make_great () that modifies the list of magicians by adding the 'Great' expression to the name of each magician. Call show_magicians () to see if the list has actually changed.

I did exercise 8.9 and it was as follows:

def show_magiciasns():

   lista=['Howard Thurston', 'David Copperfield', "Lance Burton", 'Houdini',
   'David Blaine', 'Dynamo', 'Derren Brown', 'Criss Angel']

   for magicos in lista:
      print (magicos)

show_magiciasns() 

My question is to create a function that changes this list.

    
asked by anonymous 06.07.2018 / 19:19

2 answers

2

You can use a compreensão de lista to concatenate the O grande string in front of each element / magic list string, see:

lista=['Howard Thurston', 'David Copperfield', "Lance Burton", 'Houdini',
       'David Blaine', 'Dynamo', 'Derren Brown', 'Criss Angel']

print([ 'O grande ' + magico for magico in lista ])

Putting it all together:

def make_great( lista ):
    return [ 'O grande ' + magico for magico in lista ]

def show_magicians( lista ):
   for magico in lista:
      print(magico)

lista=['Howard Thurston', 'David Copperfield', "Lance Burton", 'Houdini',
   'David Blaine', 'Dynamo', 'Derren Brown', 'Criss Angel']

show_magicians( make_great(lista) )

Output:

O grande Howard Thurston
O grande David Copperfield
O grande Lance Burton
O grande Houdini
O grande David Blaine
O grande Dynamo
O grande Derren Brown
O grande Criss Angel

See working at Ideone.com

    
06.07.2018 / 23:15
0

I do not know the book but from what I understand the exercise tells you to create a new function and not modify the existing one, so you need to create this second function, make_great() , to return the magician's name with the sequence "O big "in it:

def make_great(magician):
    return "O grande " + magician

You will call it inside show_magicians() , placing it before printing the magician's name:

def show_magicians():
    # ...
        print(make_great(magicos))
    # ...

In this way, with each loop passage, the make_great() function is called with the magic name that will add the prefix "O" and return the result for the command print() .     

06.07.2018 / 21:26