The Great Gargon

I have code like:

#Print magician and their name with 'the great' in front of them
magician = ['Diedle','kuler','Gargon']
changed_magician = []
def show_magicians(mag):
    print('The magician that will be presented are:')
    for magi in mag:
        print(magi.title())

def make_great(magic,magical):
    for magici in magic:
        magici = magic.pop(0)
        magici = "The Great " +magici
        magical.append(magici)
        
        
make_great(magician[:],changed_magician)
show_magicians(magician)
show_magicians(changed_magician)

It outputs:
The magician that will be presented are:
Diedle
Kuler
Gargon
The magician that will be presented are:
The Great Diedle
The Great Kuler

I want it to output:
The magician that will be presented are:
Diedle
Kuler
Gargon
The magician that will be presented are:
The Great Diedle
The Great Kuler
The Great Gargon

Essentially, I wish there will be The Great Gargon, too. Does anyone know how to do it?

What have you tried?

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

Welcome, elite.

The issue in your code is this line:

magici = magic.pop(0)

Try another method to access the values in magic.

Why are you passing magician into the function make_great like this:

make_great(magician[:],changed_magician)

Also, why are you passing two global variables into a function as arguments without needing them to be changed?
That is, you could just have this as the last line in your make_great function:

changed_magician.append(magici)

Have a refresher of working with arrays in Python

Hope this helps

I thought the same thing as you did. However, I really don’t know what to this line:
magici = magic.pop(0)

Well, think about what you are wanting to do:

  • Add "The Great " infront of every one of your magicians.

What this line of code is doing:

def make_great(magic,magical):
    for magici in magic:
        magici = magic.pop(0)
        magici = "The Great " +magici
        magical.append(magici)
  • Looping through every magician in your array and storing them in a variable called magici :ballot_box_with_check:
  • Re-assigning the variable magici to be equal to what .pop() returns

Let us go through what the pop method does on an array.

  • pop accepts an integer as an argument, and uses that integer to index into the array it is invoked upon (magic)
  • pop then removes the element located in that index from the array
  • pop then returns the element it removed

A simple example:

arr = [1, 2, 3, 4]
for element in arr:
  print(element)
  arr.pop(0)
print(arr)

Here is the output:

1
3
[3, 4]

Is that what you expected? If not, use this tool to visualise the execution:
http://pythontutor.com/visualize.html#mode=edit

It is looping through the array, but pop is removing elements from the array. So, you cannot loop through all of them.

I highly recommend you go over the documentation for the pop method, as well as for..in loops, as well as how to get an element from an array.

Hi, thanks for the feedback. I am a beginner of Python. At this moment I really don’t know any line that will achieve my objection. I believe you know something. Do you mind give me the code directly?

We won’t just give you the answer but we can help you get there. What can I help you understand that’s been said so far?

···
arr = [1, 2, 3, 4]
for element in arr:
print(element)
arr.pop(0)
print(arr)
···
I thought the output should be:
1
2
3
4