Intensive python course by Eric Matthes ex8.10

Asked

Viewed 254 times

2

I’m having difficulty doing an exercise of the book 'Eric Matthes' intensive python course the exercises is as follows:

8.10 - Great Magicians: Eat with a copy of your 8.9 exercise program. Write a function called make_great() that modifies the list of magicians by adding the 'O Grande' expression to each magician’s name. Call show_magicians() to see if the list has actually been 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 to change that list.

  • Already solved exercise 8.9? I hope so, then edit and enter the code. Enjoy and describe what was your difficulty in solving this exercise.

  • 1

    Victor, there is the [Edit] button to add information to the question. Also, it will be interesting for you to do the [tour] and read the [Ask] guide. Other information you can find at [help].

2 answers

2

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

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) )

Exit:

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 on Ideone.com

  • guy I tested here and ta all right it’s worth, I’ll study more this code because this part of functions with lists ta frying to my brain.

0

I do not know the book but as I understood the exercise tells you to create a new function and not modify the existing one, so you need to create this second function, the make_great() to return the magician’s name with the sequence "The Great " in it:

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

You’ll call her inside show_magicians(), putting it before printing the magician’s name:

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

Thus, with each passage of the loop, the function make_great() is called with the name of the magician who will add the prefix "The Great " and return the result to the command print().

Browser other questions tagged

You are not signed in. Login or sign up in order to post.